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

You mistake is here: dynamicArr ( dynamicArr& arr) {myLinkedList(arr.data);} What is happening is this. You are calling the copy constructor that MUST construct everything in the class BEFORE it gets to the part in the { }.

In the { }, you construct a copy, but then it goes out of scope..., leaving you with an empty list.

So do this: dynamicArr(const dynamicArr& arr) : myLinkedList(arr.data) {} That way you will not have to construct an empty linked list

You will have to fix myLinkedList as well.

P.S. PLEASE write an assignment operator like this:

dynamicArr&
dynamicArr::operator=(const dynamicArr& A)
{
   if (this!=&A)
     {
       // Your stuff here
     }
   return *this;
}

ALWAYS write a copy and assignment operator for all classes that have any dynamic memory, and then for most other classes. [Note: if you don't think it will be used then put the definition in the private section of the class]

You mistake is here: dynamicArr ( dynamicArr& arr) {myLinkedList(arr.data);} What is happening is this. You are calling the copy constructor that MUST construct everything in the class BEFORE it gets to the part in the { }.

In the { }, you construct a copy, but then it goes out of scope..., leaving you with an empty list.

So do this: dynamicArr(const dynamicArr& arr) : myLinkedList(arr.data) {} That way you will not have to construct an empty linked list

You will have to fix myLinkedList as well.

P.S. PLEASE write an assignment operator like this:

dynamicArr&
dynamicArr::operator=(const dynamicArr& A)
{
   if (this!=&A)
     {
       // Your stuff here
     }
   return *this;
}

ALWAYS write a copy and assignment operator for all classes that have any dynamic memory, and then for most other classes. [Note: if you don't think it will be used then put the definition in the private section of the class]

I did the following:
- changed "class dynamicArr{" with "class dynamicArr : public myLinkedList {" because it gave an error about type `class myLinkedList' is not a direct base of `dynamicArr'.
- changed the line "dynamicArr ( dynamicArr& arr) {myLinkedList(arr.data);} " with dynamicArr(const dynamicArr& arr) : myLinkedList(arr.data) {}

Now the output is:

Linked List is created with default constructor
Linked List is created with default constructor
errorLinked List is created with copy constructor
Linked List is created with default constructor
errorThe Linked List is empty!!
Linked List is deallocated!!
Linked List is deallocated!!
Linked List is deallocated!!

So as you see, it now didn't goes into the destructor and goes into only default constructor additionally. So now why is that??

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();}

You were correct to make dynamicArr a derived class of myLinkedList. Once you have done that, you don't need to have a member of dynamicArr that is an instance of myLinkedList. One huge problem exists in your copy constructor of myLinkedList. When you detect that the other linked list is not empty, you create many copies of the data found in the other linked list, but you never anchor those nodes to the copy. So, head is still pointing at NULL, your temp variables go out of scope, and you have a massive memory leak and an empty list. You should modify the copy constructor to look more like this:

myLinkedList ( const myLinkedList& other )
{
    if( other.head == NULL )
        head = last = NULL;
    else
    {
        head = new node( other.head );
        node* temp = other.head;
        tail = head;
        while( temp->next != NULL )
        {
            temp = temp->next;
            tail->next = new node( temp );
            tail = tail->next;
        }
        cout << "Linked List is created with copy constructor" << endl;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.