Static member variables of class type

Started by
29 comments, last by Decrius 13 years, 7 months ago
For static member variables, where are they constructed, compile time or run time? For instance below, is 54 initialized at compile time or is it executed in code somewhere (and where would that be executed if so).

When I have a member variable of a class type, it cannot be constructed like the integer in a cpp. Where would I construct it?


Class A
{
public:
A();
};

Class B
{
public:
static A a;
static int myInt;
};

B::myInt = 54;
//A::a(), A::a = A(); ???

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Advertisement
A static member variable is initialized outside the class,

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr038.htm
This does not work for variables of a non-default type though. IE it gets created through a constructor, unlike the type 'int'.

Therefore calling:

B::a = A();
B::a = new A(); //obviously wont work anyway

Just nothing works. The only way to construct it I have found is in a code block:

main()
{
B::a = A();
}

This leads to two objects instead of 1 though....

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

What language is this supposed to be? C++? There's no keyword "Class" in C++...

Anyway, this works in C++:
class A { public:  A();};class B { public:  static A a;  static int myInt;};int B::myInt = 54;A B::a;
C++ FAQ - the page linked contains a lot of information about initializing static variables and the gotchas.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

C++

And the post by alvaro does not work as I have tried it and just tried it again. No variables in that class are being set through the constructor. What constructor is called? I have provided my own default constructor, and it is not calling that constructor.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

It may be calling the constructor, but after you are checking for it. The order of dynamic initialization for objects with static storage duration can be quite chaotic.
I can make them pointers and its fine. The non-pointer version I have to create 2 copies of the object as I posted before:

void main()
{
B::a = A();
}

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Why do you want a static in the first place? They are a poor solution most of the time; only occasionally useful. Odds are then that there's a better solution than using a static for your particular problem.
Quote:Original post by dpadam450
C++

And the post by alvaro does not work as I have tried it and just tried it again. No variables in that class are being set through the constructor. What constructor is called? I have provided my own default constructor, and it is not calling that constructor.


Can you please post the exact code you are running. And this time copy and paste, so we don't get "Class" or other unrelated problems.

This topic is closed to new replies.

Advertisement