Class Deconstructor

Started by
17 comments, last by Mat1515 20 years, 1 month ago
quote:Original post by paulsdsrubbish
You cannot accurately predict what order static object instances will construct and destruct.


Within a single translation unit (single cpp file + everything you #include), objects are initalized in the order in which they are defined. Destructors run in reverse order (see my previous post).

Across multiple translation units, the order of initialization is undefined. That is, you cannot control whether the objects defined in foo.cpp will be constructed before or after those in bar.cpp

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Hey, I know this is a little late, but this is the code I was using when the deconstructor didn't appear to work:

#include <iostream>using namespace std;class Object {public:    Object() { cout << "creating object" << endl; }    ~Object() { cout << "deleting object" << endl; }};Object myGlobal;int main(){    Object myLocal;    return 0;}


There is only one 'deleting object' line. I picked up on this from a book called C++ in Action - here is the page where he talks about the issue. (Look at the bottom of the page under 'Troubleshooting Tips' for his specific reference to VC++ 6)

Perhaps I AM doing something wrong, but please, tell me what it is!

Henry

[edited by - Henners on March 10, 2004 1:28:37 PM]
---------------------------------Use Firefox, you'll never ever look back.
I have 1 global object, the main game app itself. Its destructor gets called just fine when I quit. And no I''m not using new/delete, it''s static.
Maybe by the time the process gets around to calling your destructor, it is no longer really attached to the console window, because of cleanup code in the executable, and thus the global object''s destructor isn''t really capable of writing to the console window. Try opening a file, writing to it, and closing it in the destructor.

int main() { return *((int*)0); }
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Thank you Agony,

I opened a file and found the ctor and dtor to work whether global or local scope.

So when Mr. Eckel refers to MSVC++ 6 having a sub-standard library, does he mean that the cleanup code in the executable could be made so that the thread does not detach before all objects are destroyed? I assume this is the responsibility of the library, seeing as how I don''t do it

Finally, an earlier post mentioned that you shouldn''t use global objects. Why is that? If I want to have a global application object like aldisd uses, is that necessarily bad programming practice?
---------------------------------Use Firefox, you'll never ever look back.
Actually, what happens is that the std::cout-object is destroyed *before* your object. If you use printf() you''ll see it works.
quote:Original post by downgraded
Don''t use global objects!


Don''t use VC++ 6!

VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux
In reference to global variables, I''d say this: don''t use a lot of trivial global objects. But a few objects that are a central part of your program can be fine, such as a game object. The problem of global objects is usually that they tend to be used to pass information around all over the place in an ugly fashion that is more likely to cause bugs, but when you just have a few objects, you''re typically not using them for that purpose.

int main() { return *((int*)0); }
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
quote:Original post by amag
Actually, what happens is that the std::cout-object is destroyed *before* your object.

Doh! I should have known that. Silly me.



int main() { return *((int*)0); }
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement