Archived

This topic is now archived and is closed to further replies.

Visual C++ 6.0 - static member initialization problem

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have implemented a system of replicators for some classes in Visual C++ 6.0 but some of the static replicators do not get initialized depending on the .cpp where I place them. In .h I declare a static member for the replicator (using a #define): class cMyClass { static cRep m_rep_cMyClass; ... } where class cRep { ... int index; ... } Then in .cpp I implement it with a #define that on short do like this: cMyClass::m_rep_cMyClass = { ..., cReplicators::Add( &cMyClass::m_rep_cMyClass ),...} cReplicators keeps a static list with pointers to the replicators from each class that was registered. As I declare more classes, I expect them to have theirs replicators in the replicators list. The problem appear in some .cpp where the execution of code ( before calling the main() function ) doesn''t seems to reach. I have test it by placing a breakpoint in cReplicators::Add and look for each class that I want to be registered. When I place the implementation in other .cpp they do registered fine! I also tested with a simple global int like this: int g_global = GlobalFuncion(); and in the .cpp with the problem the execution doesn''t reach GlobalFunction(). Is there a reason for Visual C to skip my initialization of static members ? Is there a problem with some order of executing the code mefore calling the main() ? Please help! Thank you!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Do you initialise your static member somewhere ?
that :
class cMyClass
{
static cRep m_rep_cMyClass;
...
}
is just a declaration...

hope it helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Oopps...

I misread your message, and yes, the definition is done...

Another try :
Is the cpp causing problem in a different lib ?
VC seems to "optimise" some vars a little too agressively in these cases.

Share this post


Link to post
Share on other sites
The replicators and the classes I expect to registers are all in the same lib. This lib is used in a main application where I try to create classes using those replicators.
However since the declarations and implementation of the replicators are in the same lib I don''t know why some work and some not.
More exactly, I have declarations in
in cMyClass1.h, cMyClass2.h, etc
and inplementations in cMyClass1.cpp, cMyClass2.cpp, etc
When this failed for some replicators, I''ve moved the implementations in another cpp cMyClasses.cpp

It might be a problem of Visual C++ optimization...
Do you know a way to force it? Maybe if I use them somewere explicite. Now, they are all declared and implemented using defines and I use them throught functions from the replicator manager.

Share this post


Link to post
Share on other sites
They are all static and I don''t depend on them, I just add pointers in a static array from cReplicators.

However I think it might be a sort of optimization or a bug from visual C, because I used the replicator in my main project (not in the lib where it was declared and initialized) and it worked.
I think it was skipped by Visual C++ for some reason.
All I did to fix it was a simple global int gg = cMyClass::m_rep_cMyClass.idx and Visual C got the idea that I need that static
And I say it might be a bug or something, because I was on debug with no compiler optimizations.
Thank you for helping me on this forum!

Share this post


Link to post
Share on other sites
many compilers have this ''bug''. (
one way around it, if you want a static member in a class is:

class Foo {
int bar() {
static int m_bar;
return m_bar;
}
}

so then you refer to Foo::bar() when you want to get the static member variable, and this will ensure that it is created.

Share this post


Link to post
Share on other sites