I have base class that has a protected static member variable.
I'd like to access that variable from a non static function from within a class that inherits the base class. How would I do that?
for example:
class State
{
public
...
protected:
static SceneManager * gSM;
...
}
class GameState : public State
{
public:
void function();
...
}
void GameState ::function()
{
State::gSM->doSomething();
}
This gives me linking errors - unresolved symbol. So any ideas?
Accessing static member variables
-
- Halfling
- Posts: 60
- Joined: Fri Oct 24, 2003 6:39 pm
- x 1
-
- Halfling
- Posts: 85
- Joined: Tue Jun 10, 2003 2:57 pm
-
- Halfling
- Posts: 60
- Joined: Fri Oct 24, 2003 6:39 pm
- x 1

Code: Select all
class State
{
public
...
protected:
static SceneManager * gSM;
...
} ;
//**********FORGOT this line below**********
SceneManager * State::gSM = NULL;
class GameState : public State
{
public:
void function();
...
}
void GameState ::function()
{
State::gSM->doSomething();
}
-
- Kobold
- Posts: 31
- Joined: Fri Feb 17, 2012 10:09 am
- x 5
Re:
saetrum wrote:Code: Select all
class State { public ... protected: static SceneManager * gSM; ... } ; //**********FORGOT this line below********** SceneManager * State::gSM = NULL; class GameState : public State { public: void function(); ... } void GameState ::function() { State::gSM->doSomething(); }
This piece of code helped me to solve an important issue, i give you KUDO for this ! (idea to have NULL for scenemanager was not so evident in the fire of action)