Jump to content
  • Advertisement
Sign in to follow this  
dpadam450

Static member variables of class type

This topic is 2859 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

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(); ???

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!