Copy Constructor

Feb 5, 2013 at 8:43am
.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().
Feb 5, 2013 at 9:00am
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;
        }

Feb 5, 2013 at 9:09am
No, I am trying to copy data to the other list. Not form the other list
Feb 5, 2013 at 9:16am
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.
Feb 5, 2013 at 9:17am
First thing if you are using Template for your program than make sure that Declaration and Definition must go in the same file...
Feb 5, 2013 at 9:19am
Not necessarily I can call .h file in the .cpp like
 
#include "---.h" 
Feb 5, 2013 at 9:21am
@guestgulkan: can you please explain how?
Feb 5, 2013 at 9:26am
Hey Farrukh i know that...
please read Templates and multiple-file projects From
http://www.cplusplus.com/doc/tutorial/templates/
Feb 5, 2013 at 9:47am
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.)
Feb 5, 2013 at 9:53am
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!
Feb 5, 2013 at 10:00am
Thank You All of You, I found my mistake
Topic archived. No new replies allowed.