Hello, I want to create a copy constructor that copy two queues that have objects in its cells, to make this I need a copy constructor to clone the objects from one queue, and then copy this objects into the new queue. To clone the objects, I would create a pointer, point to the object of the old queue, and then asign this pointer to the new queue. The problem is that there are going to be two pointers pointing to the same object, so what I would like to do is to create objects that are equal, but independent one from each other, to avoid a mess with the pointers. What I have been thinking is something like this:
The queue is made of objects of type Component
1 2 3 4 5 6 7 8 9 10 11
Queue::Queue(Queue& theQueue){
//an iterator that points to the first element in the queue
Iterator iterator(this->first);
while (iterator.actual!=0){
Component* comp = iterator.actual->element;
Cell* cell = new Cell(comp);
theQueue.add(cell);//IN BOTH QUEUES THER ARE THE SAME OBJECT!!
iterator = iterator.next;
}
}
Without more info on your Queue and Iterator classes the basic idea of what you are doing looks OK. If you want more detailed comments then you need to post the Queue class definition and your Iterator class.