Static members in derived classes

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?

I hope that makes sense...
Some of that does not make sense, but is this feasible:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Base {
public:
  virtual size_t count() const = 0;
};

class Foo : public Base {
  static size_t fcount;
public:
  Foo() { ++Foo::fcount; }
  virtual size_t count() const { return Foo::fcount; };
};

class Bar : public Base {
  static size_t bcount;
public:
  Bar() { ++Bar::bcount; }
  virtual size_t count() const { return Bar::bcount; };
  static size_t countB() { return Bar::bcount; };
};

Foo::fcount = 0;
Bar::bcount = 0;

int main() {
  Base * p = new Foo;
  p->count();
  Bar::countB();
  return 0;
}
Last edited on
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(unsigned int spriteId, float width, float height, float movementSpeed);
	~PlayerSprite();
	
private:
	static int playerSpriteNumber;

Is the header, the PlaerSprite cpp has

1
2
3
4
5
6
7
8
9
10
11
12
int PlayerSprite::playerSpriteNumber = -1;

PlayerSprite::PlayerSprite(unsigned int spriteId, float width, float height, float movementSpeed)
	:BaseSprite(spriteId, width, height, movementSpeed)
{
	playerSpriteNumber++;
}

PlayerSprite::~PlayerSprite()
{
	playerSpriteNumber--;
}


In the enemysprite code:

1
2
3
4
5
6
7
8
9
10
11
class enemySprite : public BaseSprite
{
public:
	enemySprite(unsigned int spriteId, float width, float height, float movementSpeed);
	~enemySprite(void);


private:
	static int enemySpriteNumber;

};


Is the header and:

1
2
3
4
5
6
7
8
9
10
11
12
13
int PlayerSprite::enemySpriteNumber = -1;

enemySprite::enemySprite(unsigned int spriteId, float width, float height, float movementSpeed)
	:BaseSprite(spriteId, width, height, movementSpeed)
{
	enemySpriteNumber++;

}

enemySprite::~enemySprite()
{
	enemySpriteNumber--;
}


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




1
2
3
// enemysprite.cpp
int PlayerSprite::enemySpriteNumber = -1;
    ^^^^^^^^^^^^

enemySprite::

Note: the C2039 should mention linenumber, even if there were no IDE.
That is a link to a screenshot of the IDE error and undline squigly if it helps.

http://vita-obscura.com/images/error.png
Lets rephrase: Why does your enemysprite.cpp contain word PlayerSprite?
Oh my god.... Now I just feel stupid >.<

Ok now it works - thank you for your time and I am glad I posted in beginners section >.>
Topic archived. No new replies allowed.