I have trouble understanding why the following code doesn't construct and destruct the two objects I create the way I'd expect:
#include <iostream>
class MyClass {
int myVar;
public:
MyClass(int x) {
myVar = x;
std::cout << "constructing " << myVar << ", " << (long)this << std::endl;
}
~MyClass() {
std::cout << "destructing " << myVar << ", " << (long)this << std::endl;
}
};
int main(int argc, const char * argv[])
{
MyClass a = MyClass(1);
a = MyClass(2);
return 0;
}
I'd think that inside main I first create an object with the value 1, then create a new one with value 2. And each of the objects gets constructed and destructed, so that I'd expect to see the following output:
constructing 1, 3456
constructing 2, 6789
destructing 1, 3456
destructing 2, 6789
However, I get this:
constructing 1, 3456
constructing 2, 6789
destructing 2, 6789 <- note the "2"
destructing 2, 3456
Update: I've added output of the object's address (this) so that one can better see which object does what.
When I use "new MyClass" instead, I do not run into this weird effect.
What is causing this, and, understanding my goal, what is the right way to avoid similar mistakes in the future?
While this example looks harmless, I ran into crashes of my code because I had allocated other objects in the constructor and freed them in the destructor. This did lead to freeing the objects when the object was still in use.
Conclusion
Now that all my questions are answered, let me summarize:
The line a = MyClass(2);
will not call a destructor, it will call the assignment operator (MyClass::operator=
) which you have not implemented, so the compiler provides one for you - it doesn't "print" anything, so you don't see that.
The reason you get destrucing 2
twice is that immediately after the line a = MyClass(2);
, the temporary MyClass(2)
object is destroyed. Then at the end of main
the variable a
is destroyed, and since myVar
is now 2, it prints 2 again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With