I am new to C++ and I wrote a small program to learn about how assignment works with objects. I was prompted to do this from the cpp docs at this page (http://www.cplusplus.com/doc/tutorial/classes2/). On this page it states:
The implicit version [of the copy assignment operator] performs a shallow copy which is suitable for many classes, but not for classes with pointers to objects they handle its storage. In this case, not only the class incurs the risk of deleting the pointed object twice, but the assignment creates memory leaks by not deleting the object pointed by the object before the assignment.
The final part which I have formatted in bold is why I had decided to test things out. I thought that this problem could be solved by handling the deletion of pointed objects in the destructor (which is standard?), rather than having to overload the copy assignment operator. If the destructor is not being called, then isn't that really inconvenient? Say I had multiple referenced objects, I would have to put all the deletes in both the destructor (for most cases of reallocation) and also in the assignment overload.
During this test, I encountered an entirely different problem. My initial idea was to create a simple class which stores an int (as an identifier for testing purposes) and overload the constructors and destructors to see when and if the destructor was being called.
Here is my code:
class Test{
public:
int id;
explicit Test(int id) : id(id) {
cout << "Created " << id << endl;
}
~Test() {
cout << "Destroyed " << id << endl;
}
};
int main() {
Test x = Test(1);
x = Test(2);
cout << x.id << endl;
return 0;
}
The output I would have expected was to be:
1: Created 1
2: Destroyed 1 ? (this was the one I was unsure of as the website hinted this destructor would not be called if the object was 'replaced' with another rather than going out of scope)
3: Created 2 Object 2 'replaces' object 1 as it is assigned to x
4: 2 the value of Object 2's id printed out
5: Destroyed 2 Object 2 destroyed as it goes out of scope
Instead, i got the following output:
Created 1
Created 2
Destroyed 2
2
Destroyed 2
This really does not make sense to me.
Using the debugger, Created 2 and Destroyed 2 both display when the line x = Test(2); is called. If we just assigned x to Object 2, why is its destructor being called immediately? This follows on to the next part.
Secondly, since the destructor for Object 2 has been called, we can assume it has been destroyed. The next output of 2 seems to contradict this, as it suggests that x is still holding Object 2 (expected, but contradicted by the call of its destructor).
I'm not too sure why this is happening.
Finally, Destroyed 2 is outputted. This would make sense if we didn't see this earlier. Object 2 is stored in x, so when it goes out of scope the destructor is called.
For some reason we get the destructor called twice, and Object 1, which is 'overridden' by the assignment of Object 2 to x never has its destructor called, but instead the destructor of the object we just created has its destructor called.
So... this sums up to a two-part question:
1: Why is this weird behaviour occurring, and is there any logical reason to why it is?
2: Does 'overwriting' an object (e.g. Object 1) with another object (Object 2) by assignment lead to its destructor (in this case the destructor of Object 1) to be called or not?
Thanks in advance.
thisas well asid- Tim Randall