I am writing my first program for studies (a game) and I have set up a base sprite class which then has a number of derived sprites, player, enemy etc. I was wanting a static variable in the player derived class that automatically keeps track of how many player instances have been created - not so important for player as there is only two, more so for enemies. It appears however, that even if the static is declared and initialised in the player derived class the enemy derived class still sees it and cannot use it.
I tried creating a new named one in the enemy class only now it gives errors that the playersrpite has no member of that name. Is there a relatively simple way - I am not that clued in yet - where I can have a single static variable that is unique to each derived class without having to include the variable in all the derived classes that don't use them?
It appears, correct me if I am wrong, that you have declared a static member in Foo and a static member in Bar with different names (fcount, and bcount). That was exactly what I tried to do by my compiler is throwing an error. Using the same names above, when I try to initialise "bcount" for instance in the Bar class it has an underlined squigly that reads "Error: class Foo has no member "bcount".
I will give some code to make it easier for you and me to follow BaseSprite is the base class and with all the stuff not necessary removed:
1 2 3 4 5 6 7 8 9 10
class PlayerSprite : public BaseSprite
{
public:
PlayerSprite(unsignedint spriteId, float width, float height, float movementSpeed);
~PlayerSprite();
private:
staticint playerSpriteNumber;
Is the cpp - in both cases the idea being that whenever a new sprite of that derived class is created it tracks exactly how many are created then when they are destroyed the number goes down. Except in the enemsprite cpp file, it has a squigly:
error C2039: 'enemySpriteNumber' : is not a member of 'PlayerSprite'
Now I am still learning code so the above you posted is not entirely clear to read but outside of setting up the virtual getter functions, I think thats what I have already....