I’m having a hard time getting around the pointers. I can imagine someone here can make conceptualizing the copy constructor much more intuitive. I understand that you can’t simply assign pointers to each other (shallow copy), but I’m having a hard time actually copying the objects.
Here’s a sample of my code:
class LList {
/* stuff */
private:
struct node {
node *next;
node *prev;
int *o;
};
node *first; // The pointer to the first node (NULL if none)
node *last; // The pointer to the last node (NULL if none)
}
Thanks for the help!
When you are writing the copy constructor,
X(const T& other), for an object,X, that contains a dynamically allocatedT* p(probably aNode*in your case), you will likely end up writing a line like this:This creates a copy of the object being pointed to by the object you’re copying, giving you a deep copy of that object. In your case, this would end up in a recursive deep-copying of the list.