-4

I have a static variable of a class in another class.

class1.h

class1
{
public:
static class2 check;
}

class2.cpp
class2 class1::check;

Now class2.cpp has the following default constructor

class2()
{
  size = G_SIZE; //this G_SIZE is an extern variable, which gets initialized after main() is called.
}

Since the static initialization happens before the extern variable is initialized, I get 0 initialized to size. How do I handle this?

EDIT : Not sure why it is being downvoted.

user2761431
  • 925
  • 2
  • 11
  • 26
  • did not fully understood you - what is the problem in here ? – TarmoPikaro Apr 17 '16 at 19:34
  • class2 default constructor gets called during static member initialization and this happens before main is called. Due to this, size variable doesn't get initialized with the value I want (G_SIZE). Since G_SIZE gets its value only after main() is called. How do I get size to be initialized with the right value. – user2761431 Apr 17 '16 at 19:38
  • `G_SIZE` is initialised *before* `main` is called, as is `class1::check`. The order of those initialisations is unspecified if the variables are defined in different translation units. – molbdnilo Apr 17 '16 at 19:38
  • @molbdnilo: _"`G_SIZE` is initialised before `main` is called"_ Eh? How do you know? Looks like OP is misusing "initialised" to mean first assignment, but beyond that there's no way to know when this occurs other than to take the OP at their word. An [MCVE] would be nice. – Lightness Races in Orbit Apr 17 '16 at 19:40
  • @molbdnilo I missed a info, G_SIZE is read from a file and hence,it is called after main(). But, I get your point. – user2761431 Apr 17 '16 at 19:41
  • You could post vaguely realistic, syntactically valid code so that people don't have to guess what you're trying to say. – juanchopanza Apr 17 '16 at 19:43

2 Answers2

1

You should never have static variables depend on other variables (especially other static variables). But that is exactly what you are doing.

The first recommendation: Don't do it. Find a better design.

The next recommendation: if you must do it (and in this case, most certainly you do not), you can wrap your access in a function, which will delay until runtime, when the values are guaranteed to be setup. See here: What is the lifetime of a static variable in a C++ function? and here: C++ static initialization order

Community
  • 1
  • 1
johnbakers
  • 24,158
  • 24
  • 130
  • 258
0

By telling compiler 'static' - you will most probably get that variable initialized during mainCRTstartup - that's before main is called. If you're performing variable initialization during main, other data initialization must happen after that - you could have doInitialize() like function call, which would perform initialization of your 'check' variable.

It's also possible to have static written inside function itself - then static gets initialized when that function gets called - you could place 'static' after you initialize G_SIZE in main itself.

Playing around with order or constructors / destructors is always bit of luck game (Dangerous) - order to constructors and destructors is not guaranteed, and it's possible that you'll hit uninitialized data when initializing in static - simply because another variable was not initialized yet.

But sometimes even destructor call order might needs to be altered. -

FYI:
http://www.codeproject.com/Articles/442784/Best-gotchas-of-Cplusplus-CLI
CrtDestroyStatics.
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62