it is saying that if you are passing a very poorly coded 'dumb' object around, it will screw up.
this will NOT happen if you pass in, for example, a <vector> or <string>: these objects were coded not to be idiotic and will NOT do what the book said.
but say you did this:
1 2 3 4 5 6
|
struct derp
{
int * ip;
derp() {ip = new int[100];}
~derp(){delete[] ip;}
}
|
you did not manually add assignment operators, so when it does a copy, which uses assignment, it will use the default one. The default one will just copy the pointer, not make a new one. But when it is destroyed, it will deallocate it, and that pointer was being used by both the copy and the original. Its a bug / badly coded object across the board: it is also true that if you had a copy and modified the 'array' under the copy, you modified the data in the original as well, its the SAME data. This kind of class can introduce all kinds of 'fun' bugs due to this problem.
The fix for it is to do a rule of 3 or rule of 5 for the class if you need to make copies of it. (you may also be able to do some sort of static class member variable voodoo that can 'know' if the current object called the constructor and if it did not, do not call the destructor, but its a lot of smoke and mirrors to solve a part of a bigger problem).
you should be seeing these rules soon in your studies. And the real fix for it is to not make classes that manhandle memory if you can avoid it, eg like using a vector instead of a pointer above would solve the problem without any extra nonsense. If doing memory yourself is absolutely a must, then you need to be very, very careful with it.
the reason it does not allocate new memory is the copying does not call the normal constructor, it calls the copy constructor, which does not exist (was not programmer written) so it generates one and the generated one is not correct for this kind of object.
Or is it at the same memory location because it is a bitwise copy of the original? |
almost this. As you are thinking, yes, this is why, but I am not 100% sure that all possible automatically generated functions would be purely bitwise copies ... I need to think about that one.
if you want to see this in action, use my struct, add a cout statement to the destructor, and play with it. if you call it in a function that does a non-reference parameter of the struct, it will destroy the object twice (you can see in the print statements).