how can i add copy constructor to this class

this is my class there is a problem with my copy constructor .. is that correct ??

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
46
47
48
49
50
51
52
53
54
55
56
57
58

struct Node
{
Type info;
Node<Type> *next;
};

template <class Type>
class Queue
{
Node<Type> *front;
Node<Type> *rear;

public:
Queue();
~Queue(); 



void enqueue( const Type & numba);

bool dequeue( Type & numba);

bool peek( Type & numba) const;

bool isEmpty() const
{
return front == NULL;
}

void makeitEmpty();

void print()
{
cout << "queue: ";
Node<Type> *pointer1= front;
while ( pointer1!= NULL )
{
cout << pointer1->info << "->";
pointer1= pointer1->next;
}
cout << "NULL" << endl;
}

};

template <class Type> 
Queue <Type>::Queue()
{
front = NULL;
}

template <class Type> 
Queue <Type>::~Queue ()
{ 
makeitEmpty(); 
}



thanx
Last edited on
I don't see a copy constructor here. When you have member pointers, the data pointed to by those pointers is not automatically re-allocated. You therefore point at the old data which may no longer exist if you are working with the default copy constructor.

You'll need to create your own copy constructor and *front and *rear should point at new addresses. Then you need to copy the data that the original *front and *rear pointed to over to the new class manually.
thank you stewbond .. but if you dont mind ..
i tried many times to learn copy constructor but i didnt get it .. could you show me how specifically ? (show an example)
Topic archived. No new replies allowed.