Copy Constructor

.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template <class T>
struct ListItem
{
    T value;
    ListItem<T> *next;
    ListItem<T> *prev;

    ListItem(T theVal)
    {
        this->value = theVal;
        this->next = NULL;
        this->prev = NULL;
    }
};

/* This is the generic List class */
template <class T>
class List
{
    ListItem<T> *head;

public:

    // Constructor
    List();

    // Copy Constructor
    List(const List<T>& otherList);
.
.//Rest of Member Functions
.
    // Insertion Functions
    void insertAtHead(T item);

};


My .cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
template <class T>
List<T>::List()
{
    head=NULL;
}

template <class T>
List<T>::List(const List<T>& otherList)
{
    ListItem<T>* TempPointer=head;

        while(TempPointer!=NULL)
        {
            otherList.insertAtHead(TempPointer->value);
            TempPointer=TempPointer->next;
        }

}
.
.
.
template <class T>
void List<T>::insertAtHead(T item)
{
    ListItem<T>* TempHead=head;
    ListItem<T>* Temp;
    Temp= new ListItem<T>(item);

    Temp->value=item;

        if(head==NULL)
        {
            head=Temp;
            Temp->next=NULL;
            Temp->prev=NULL;
        }
        else
        {
            head=Temp;
            Temp->next=TempHead;
            TempHead->prev=head;
        }

}


Whenever I Call the following in main .cpp
1
2
3
4
5
6
List<int> List1;
.
.//Code to Fill Up List1
.
List<int> List 2;
List1(List2);


it display the following error.
 
error: passing 'const List<int>' as 'this' argument of 'void List<T>::insertAtHead(T) [with T = int]' discards qualifiers [-fpermissive]|


Note: I cannot change the prototype of the function insertAtHead().
in your copy constructor you copy the data to the other list. But you want to copy it from that list. In other words you have to wirte it like so:
1
2
3
4
5
6
7
    ListItem<T>* TempPointer=otherList.head;

        while(TempPointer!=NULL)
        {
            otherList.insertAtHead(TempPointer->value);
            TempPointer=TempPointer->next;
        }

No, I am trying to copy data to the other list. Not form the other list
Farrukh wrote:
No, I am trying to copy data to the other list. Not form the other list

In which case I think you completely misunderstand what the copy constructor is for and the circumstances in which the compiler
will call it.
First thing if you are using Template for your program than make sure that Declaration and Definition must go in the same file...
Not necessarily I can call .h file in the .cpp like
 
#include "---.h" 
@guestgulkan: can you please explain how?
Hey Farrukh i know that...
please read Templates and multiple-file projects From
http://www.cplusplus.com/doc/tutorial/templates/
The copy constructor is used to initialise
a new object under construction from
the values of an exixting object.

Example 1;
1
2
3
T someT;
 T anotherT = someT; //compiler optimisatiow call copy constructor
 


Example 2;
1
2
3
T someT;
 T anotherT(someT); //copy construct anotherT
 


The copy constructor looks something like this:
1
2
3
4
5
T::T(const T& other)
 {
    //do copying stuff
 }
 


So - the this object will be anotherT and
const T& other parameter will be the
existing object - in this case someT.

As the anotherT object is the one being created/initialised
it's values are all nonsense - the whole idea is to intitialise
it from the values of the existing object passed as the function parameter.

So copying values from anotherT (under construction) to someT (existing object) doesn't
make sense (and could cause segfaults/memory access violations.)
Copy constructors are used the entire block of memory of some object.

For example!

if you have data stored inside an object, but you want it not to be pointed out, but copied instead. you should Copy construct it.

1
2
3
4
5
Foo* CopyFoo(Foo* foo)
{
  Foo*  myNewFoo = new Foo(*foo);
  return myNewFoo;
}


myNewFoo will have a fresh copy of what you passed to CopyFoo

You have to delete the newly created object when you finish using it, since it was allocated in the heap trought new

Regards!
Thank You All of You, I found my mistake
Topic archived. No new replies allowed.