So I'm reading about copy control and the Rule of Three, and it seems that the main example they're giving as to why they're necessary is when using pointers as class members. It says that if you copy the pointer from one object to another, then multiple objects of that class are pointing to the same memory. Why is this bad? Don't we want the object we're pointing things to do point to the same thing? What exactly is supposed to be happening when pointers are copied between classes? I'm not sure exactly what we're supposed to do when using a copy constructor to deal with pointers..
-
2Seems as if your question is completely answered here: http://stackoverflow.com/questions/4172722 – Robert Harvey Nov 16 '13 at 22:14
-
There's also some help here: http://stackoverflow.com/q/57483 – Robert Harvey Nov 16 '13 at 22:15
-
@RobertHarvey geez, I'm reading the first link you posted and it explains things SO much better than my book – FrostyStraw Nov 16 '13 at 22:20
-
Books can be somewhat... academic. – Robert Harvey Nov 16 '13 at 22:25
-
In an example they give, "person(const person& that) { name = new char[strlen(that.name) + 1]; strcpy(name, that.name); age = that.age; }" instead of copying the pointer from one object to the other (and thus having both objects point to the same thing), are they instead dynamically allocating new memory that will contain the same data as the object that was copied? – FrostyStraw Nov 16 '13 at 22:29
-
You could try asking that as a new question. – Robert Harvey Nov 16 '13 at 22:31
5 Answers
You want to write a copy constructor if you have any dynamic content, in order to avoid shallow (member-wise) copy.
Why would it be a problem if different pointers are pointing to the same object? Because that way, if you change the copy, you change the original.
If you, for example, have two pointers to the same object, and then delete the object by using the first pointer - the object will be gone; the second pointer will not be null, but a dangling pointer, with its previous, but now - invalid value.

- 4,471
- 7
- 40
- 64
Consider: std::vector
contains a pointer to an array of elements. If you make a vector, then make a copy of that vector, then change the copy, you don't want the changes to the copy to also change the original. And if you insert elements into the copy, to the point where the array has to be deallocated and resized, the original vector now points to deallocated memory.
The point of the Rule of Three is to prevent copies from having any residual link to their originals. Copies are meant to be independent. There are exceptions to this (google "copy on write"), but they too require special handling and tracking -- again, in the copy constructor, assignment operator, and destructor.

- 40,271
- 12
- 71
- 104
The copy constructor is a tool that allows you to create another object of the same class by copying an existing one not by construction of new object.
If you would have copied pointers that way so they're pointing on the same place in the memory you would create another pointers to in fact the same object instead of creating a new one. It's like creating a shortcut to file in windows :-)
Copy constructor should copy every component of object into another place of memory and provide new pointers to this place, and that way create effective copy of an object.

- 1,005
- 10
- 18
If you have two object sharing a pointer then if you make a change to one object it will also change the other object. Normally (but not always) this is considered a bad thing.
Additionally if two objects share a pointer then you have the issue of who is responsible for deleting the pointer.
There is actually a standard class called std::shared_ptr
which implements shared pointers and automatically manages the memory for you. If you want a class that shares pointers then you should use this class. This way you will not need to worry about the rule of three.

- 85,011
- 4
- 57
- 81
It's bad, because when you delete
the object pointer by the pointed in one object, the pointer in the other object will point at invalid memory. That's why straight up assigning the pointer is risky. That's the most dangerous part. But that's not all. If you have two pointers that point to the same place in the memory, changing the memory via one pointer will also change the contents of the second pointer. Sometimes it can make changes in an object you won't expect to change and leave you with searching a bug for days.
And to answer to your second qestion:
What exactly is supposed to be happening when pointers are copied between classes?
Well... it depends on what you want. If you just want to point to something in the memory, and never change it, or delete it, then simply assigning one pointer to another is a valid idea. However, if you'll want to change the object under the pointer, it's much safer to do a copy of it first (via the new
operator and the assigment operator), and then point to the new object with the second pointer.

- 3,952
- 2
- 17
- 26