Ask your own question, for FREE!
Computer Science 7 Online
OpenStudy (anonymous):

http://ideone.com/zBPzc why was "Goodbye, World!" only printed once?

OpenStudy (anonymous):

http://ideone.com/REaCY here it's printed twice!

OpenStudy (asnaseer):

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.

OpenStudy (anonymous):

right; taking away the delete gives me this: http://ideone.com/VHDOT

OpenStudy (anonymous):

what if I make the balls static objects? I think it will work fine.

OpenStudy (anonymous):

http://ideone.com/JzRRO

OpenStudy (anonymous):

now I get it... it's because I never call ball2's destructor in my original post (both ball1 and ball2 are dynamically allocated)

OpenStudy (anonymous):

whereas in my last example ( http://ideone.com/JzRRO) as soon as main terminates their destructors are called.

OpenStudy (anonymous):

called automatically*

OpenStudy (asnaseer):

the order in which static objects are deleted is not usually well defined. it also goes against the principals of good programming practices.

OpenStudy (anonymous):

right.... so how do I make sure, say, the static ball1 is deleted before the static ball2?

OpenStudy (asnaseer):

delete ball1; delete ball2;

OpenStudy (anonymous):

anyway... my example probably breaks 10 different C++ proper practices (using this in class methods, etc.)

OpenStudy (asnaseer):

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.

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

I only have the 4th edition though... so I'm sure there's probably something new that isn't covered :(

OpenStudy (asnaseer):

ok - good. I guess you have to be shown the bad practices first in order to fully appreciate the good ones. :)

OpenStudy (anonymous):

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

OpenStudy (anonymous):

http://ideone.com/oRc3z C++ is acting strange now :(

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!