myLinkedList ( ) { //TESTED
head = NULL;
last = NULL;
cout<<"Linked List is created with default constructor\n";
}
myLinkedList ( myLinkedList &arr) { //TESTED
if (arr.head == NULL){head=NULL; last=NULL;}
else{
head = NULL;
last = NULL;
temp2 = new node;
temp2 = arr.head;
while(temp2!=NULL){
insertLast(temp2->item);
temp2 = temp2->next;
}
}
cout<<"Linked List is created with copy constructor\n";
}
~myLinkedList () { //TESTED
while (head!=NULL)
deletee(1);
cout<<"Linked List is deallocated!!\n";
}
and in my other class' constructor i wanted to call copyconstructor:
class dynamicArr {
private:
myLinkedList data;
public:
// CONSTRUCTORS
dynamicArr ( ) {}
dynamicArr ( dynamicArr& arr) {myLinkedList(arr.data);}
~dynamicArr ( ) {data.~myLinkedList();}
and my full main method:
dynamicArr B();
B.insert(4);
B.insert(6);
B.insert(2);
cout<<"error";
dynamicArr C(B);
cout<<"error";
C.print();
C.~dynamicArr();
system("PAUSE");
return 0;
but look out the outputs. As you see when i call "dynamicArr ( dynamicArr& arr)", it also goes into destructor and default constructor so what is this silly thing and the solution?
output:
Linked List is created with default constructor
errorLinked List is created with default constructor
Linked List is created with copy constructor
Linked List is deallocated!!
errorThe Linked List is empty!!
Linked List is deallocated!!
Linked List is deallocated!!
outputs