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 ===|
|