|
Statics and globals loose their values?
I''m having a number of annoying troubles tonight sa you might have seen...
This time it''s a static member vairable that seems to reset to 0 every time a new class is created. Imagine a reference counter for the class...
Every time I create a new Trace class (which is at the beginning of every function) the s_Indent seems be 0.Whay coudl cause this?
Many thanks
Chris
Chris Brodie
Your code seems to look okay. My guess is it could be your initialisation of the static variable. It should be done before the program starts (i.e. before the main/WinMain function).
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Make sure that in your class''s ctor you''re not resetting s_Indent to 0.. Sorry if that insults your intelligence..
.. I''ve been known to do dumb shit like that on occasion..
--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''''s" - Unknown
![](smile.gif)
--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Ok, I think I''ve tracked the problem down more specifically, seems as though my debugging was thourough enough...
Apparently the destructor is getting called right after the constructor, not at the end of the function call when it goes out of scope...
Example:
//The above code when tested as follows:
What seems to happen in main is the object gets destroyed -prior- to func1 being called. Of course the destructor decrements the static so it was appearing to be to stay at 0 all the time. Can anyone tell me what going on, or more importantly how to make this work...
Many thanks
Chris
Apparently the destructor is getting called right after the constructor, not at the end of the function call when it goes out of scope...
Example:
//The above code when tested as follows:
|
What seems to happen in main is the object gets destroyed -prior- to func1 being called. Of course the destructor decrements the static so it was appearing to be to stay at 0 all the time. Can anyone tell me what going on, or more importantly how to make this work...
Many thanks
Chris
Chris Brodie
That makes a lot more sense.
You''re not creating a permanent object; you''re creating a temporary CTrace object. I''m not sure what your TRACE macro does, so I''ll just use your class instead.
I think to get the intended behavior, you can do it this way:
If you use temporary objects like you have in your example, they are constructed and then immediately destroyed before the next line of code is executed. Instead, I used stack variables, which means they will exist (not be destroyed) until they leave scope.
Hope this helps.
You''re not creating a permanent object; you''re creating a temporary CTrace object. I''m not sure what your TRACE macro does, so I''ll just use your class instead.
I think to get the intended behavior, you can do it this way:
void func1 (){ CTrace msg ("func1"); // calls CTrace ctor} // calls CTrace dtorint main (){ CTrace msg ("main"); // calls CTrace ctor func1 (); return 0;} // calls CTrace dtor
If you use temporary objects like you have in your example, they are constructed and then immediately destroyed before the next line of code is executed. Instead, I used stack variables, which means they will exist (not be destroyed) until they leave scope.
Hope this helps.
My guess is that you are doing that :
#define TRACE(x) CTrace( "x" );
do this instead
#define CTrace(x) CTrace tracer( "x" );
that way you'll be creating a named object. An unamed object scope is implementation dependant. But named object ALWAYS died after their scope => { }
which is all what Stoffel said
Edited by - Gorg on March 27, 2001 7:58:30 PM
#define TRACE(x) CTrace( "x" );
do this instead
#define CTrace(x) CTrace tracer( "x" );
that way you'll be creating a named object. An unamed object scope is implementation dependant. But named object ALWAYS died after their scope => { }
which is all what Stoffel said
Edited by - Gorg on March 27, 2001 7:58:30 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement