http://ideone.com/zBPzc why was "Goodbye, World!" only printed once?
Looks like a race condition during program termination. In the first case, the program terminated before the output from the destructor of the class Ball had a chance to make it to the output stream.
what if I make the balls static objects? I think it will work fine.
now I get it... it's because I never call ball2's destructor in my original post (both ball1 and ball2 are dynamically allocated)
whereas in my last example ( http://ideone.com/JzRRO) as soon as main terminates their destructors are called.
called automatically*
the order in which static objects are deleted is not usually well defined. it also goes against the principals of good programming practices.
right.... so how do I make sure, say, the static ball1 is deleted before the static ball2?
delete ball1; delete ball2;
anyway... my example probably breaks 10 different C++ proper practices (using this in class methods, etc.)
if you are familiar with them, then you may want to consider using the shared pointers provided by the boost library to manage memory for you.
I'm still in like the first few chapters of my C++ primer book. I'm sure I will eventually learn about proper coding practices (RAII, etc)
I only have the 4th edition though... so I'm sure there's probably something new that isn't covered :(
ok - good. I guess you have to be shown the bad practices first in order to fully appreciate the good ones. :)
I might want to get this new book: http://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113 which will release after half a year :-P
Join our real-time social learning platform and learn together with your friends!