Skip to main content
GameDev.net gamedev.net
🔒 Locked

Static initialization problems (in VC++ 6.0)

Started by Kevlar-X Dec 14, 2002 at 3:22 AM 2 replies 700+ views
Original Post
Kevlar-X
Kevlar-X
Arg. I hope some of you big brained C++ guru's can help me with this one, my smooth round brain cant seem to figure it out! The actual code too big to post, so i'll just explain my problem in a general way here. .... here goes! (takes deep breath.) ------------- There is a virtual class 'Super' and it contains one member, a static variable lets call the variable 'm_SomeVar'. Now, there exists another class 'Derived'. It is derived from 'Super'. Its constructor modifies the static variable 'm_SomeVar' in Super. As far as I know, so far everything is legitimate and valid. Now.. Where my problem occurs is if I create a static instance of 'Derived' -- it gives me crazy access violation errors when its constructor is executed. It seems as though 'm_SomeVar' has not yet been fully created EVEN THOUGH the constructor for 'Super' has already been called. Its my understanding that the order in which static variables are initialized is undefined by the C++ standard, BUT ... arent static members of classes initialized before static members of derived classes? If you think you can help me, but need me to clarify or rephrase just ask. --------- I have been struggling with this problem for months, so I would really appreciate it if you could help me determine: I) What is going on exactly. II) If this is a logic error on my part, or a compiler problem. III) If its a logic error, how else can I do this? IV) If its a compiler problem, how do I work around it!? Thanks in advance! - Jacob [edited by - Kevlar-X on December 14, 2002 4:24:16 AM]
"1 is equal to 2 for significantly large quantities of 1" - Anonymous
Gee
Gee
Can you post the exact error message perhaps?
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
Fruny
Fruny
quote:
Original post by Kevlar-X
Its my understanding that the order in which static variables are initialized is undefined by the C++ standard, BUT ... arent static members of classes initialized before static members of derived classes?



Static members are initialised at program startup, in an undefined order, just like all other global variables. You need to use a Singleton pattern to make sure the base class'' static is initialised when you try to access it.

example:

class Foo
{
public:
static int& GetSomevar()
{
static int SomeVar = 42;
return SomeVar;
}
}


The static in the function will get created the first time it is accessed.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"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
Kevlar-X
Kevlar-X
Cool. That seems to make sense.

I will try changing the static member var in the class into a singleton style accessor method to a var.

I'll post back later today with the results. It sounds like it will work, and with little trouble.
Thanks Fruny!

[EDIT]
Wahoo. That solves my problem. Silly I didnt think of it earlier.
Well, in any case thx!

Now you probobly wonder what I needed all that crazy staticness for?
I have a networking based app that handles text commands. Each command is handled by a specific class that is derived from a generic Command Handler class.

I wanted a valid map filled with strings and pointers to handlers at run time. And I didnt want to modify existing code every time a new command handler was written. Sorta like the Pluggable Factories pattern.
[/EDIT]

Well. This forum is good for something after all
- Jacob

[edited by - Kevlar-X on December 14, 2002 3:18:19 PM]
"1 is equal to 2 for significantly large quantities of 1" - Anonymous

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.