How should I initialize - problem with static member variables

Hello once again. I was learning more about this static-keyword. Interesting and useful keyword I have to admit and I learnt you can use it create class status checker -like variable, which checks how many instances of there are currently around. I though this feature would be very useful to check how many players there would be laying around, so decided to create these variables in my Player -class:
Player.h
1
2
static bool stmuiExistsThis;
static unsigned stmuiAmountThis;

This refers to the class itself & "st" refers to static storage class of a variable, for sake of consistency.

However, I had to set these static member variables public, which means anybody can modify them. Now that´s a problem. I don´t want user to modify these variables.

Now you may ask: "Why can´t you just set both static member variables private?" Well, I can´t do it, because of the following piece of code I have in my project:

Player.cpp
1
2
3
4
5
6
7
8
9
10
LABE::Logic::Objects::Characters::Player::Player(void){
	using LABE::Logic::Objects::Characters::Player;
	stmuiAmountThis++;
	stmuiExistsThis = (stmuiAmountThis > 0);
}
LABE::Logic::Objects::Characters::Player::~Player(void){
	using LABE::Logic::Objects::Characters::Player;
	stmuiAmountThis--;
	stmuiExistsThis = (stmuiAmountThis > 0);
}


And at the bottom of the file:

Player.cpp
1
2
bool LABE::Logic::Objects::Characters::Player::stmuiExistsThis = false;
unsigned LABE::Logic::Objects::Characters::Player::stmuiAmountThis = 0;


The problem is that I can´t initialize these static member variables inside my constructor, because then the destructor can´t see them & another way around. I tried it, but it says:

obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x1bc)||In function `ZN4LABE5Logic7Objects10Characters6PlayerC2Ev':|
F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|5|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiExistsThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x1c3):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|6|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x1cd):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|7|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x1d3):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|8|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x1dc):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|8|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiExistsThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x302)||In function `ZN4LABE5Logic7Objects10Characters6PlayerC1Ev':|
F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|5|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiExistsThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x309):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|6|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x313):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|7|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x319):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|8|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x322):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|8|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiExistsThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x3c3)||In function `ZN4LABE5Logic7Objects10Characters6PlayerD2Ev':|
F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|12|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x3c9):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|13|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x3d2):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|13|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiExistsThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x4c5)||In function `ZN4LABE5Logic7Objects10Characters6PlayerD1Ev':|
F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|12|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x4cb):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|13|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.o(.text+0x4d4):F:\Henri Korpela\Projektit - ohjelmointi\LABE\objects\LABE_Logic_ObjectsCharactersPlayer.cpp|13|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiExistsThis'|
obj\Debug\main.o(.text+0x15a)||In function `main':|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|7|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\main.o(.text+0x1e1):F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|9|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
obj\Debug\main.o(.text+0x22f):F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|11|undefined reference to `LABE::Logic::Objects::Characters::Player::stmuiAmountThis'|
||=== Build finished: 19 errors, 0 warnings ===|
Last edited on
Now you may ask: "Why can´t you just set both static member variables private?" Well, I can´t do it, because of the following piece of code I have in my project:


I don't see why you couldn't make them private. If only Player is accessing them (which appears to be the case), then making them private would be fine. In fact that's what I recommend.

The problem is that I can´t initialize these static member variables inside my constructor


Why would you need to? Constructors are initialized on a per-instance basis, but static exist on a broader scope. If you initialize them in your constructor you'll be re-initializing them every time an object is created which defeats the point of making them static.


Really... I fail to see the problem here...

It's as simple as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Player
{
public:
  inline static int GetPlayerCount() { return stmuiCount; }
  inline static bool DoAnyPlayersExist() { return GetPlayerCount() > 0; }

  Player() { ++stmuiCount; }
  ~Player() { --stmuiCount; }

private:
  static int stmuiCount;
};

//... in the cpp

int Player::stmuiCount = 0;
I tried it, but see what happened:

F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\..\..\..\..\..\..\Henri Korpela\Projektit - ohjelmointi\LABE\LABE_Game.h||In function `int main()':|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\..\..\..\..\..\..\Henri Korpela\Projektit - ohjelmointi\LABE\LABE_Game.h|15|error: `unsigned int LABE::Logic::Objects::Characters::Player::stmuiAmountThis' is private|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|7|error: within this context|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\..\..\..\..\..\..\Henri Korpela\Projektit - ohjelmointi\LABE\LABE_Game.h|15|error: `unsigned int LABE::Logic::Objects::Characters::Player::stmuiAmountThis' is private|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|9|error: within this context|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\..\..\..\..\..\..\Henri Korpela\Projektit - ohjelmointi\LABE\LABE_Game.h|15|error: `unsigned int LABE::Logic::Objects::Characters::Player::stmuiAmountThis' is private|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|11|error: within this context|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\..\..\..\..\..\..\Henri Korpela\Projektit - ohjelmointi\LABE\LABE_Game.h|15|error: `unsigned int LABE::Logic::Objects::Characters::Player::stmuiAmountThis' is private|
F:\Programs\CodeBlocks\share\CodeBlocks\projects\Test13\main.cpp|12|error: within this context|
||=== Build finished: 8 errors, 0 warnings ===|
Never mind, it works. I had used these variables outside the class statically, so that´s why it didn´t work.

Thanks for your effort.
Topic archived. No new replies allowed.