static variables

Feb 26, 2011 at 9:23pm
Is there a way to have a static member variable of a class and have it increment every time an object of that class is instantiated so that I know how many objects of that class are created.

I think I used to do this in Java, but I can't seem to be able to do it in C++.

so I have class:

1
2
3
4
5
6
7
class Asteroid
{
  public:
    void incrementNumOfAsteroids();
  private:
    static int numOfAsteroids = 0;
}


Then in the implementation have something like this
1
2
3
4
void Asteroid::incrementNumOfAsteroids()
{
  numOfAsteroids++;
}


but that doesn't seem to work...
Feb 26, 2011 at 9:23pm
Put the code that does the incrementing in every constructor the class has. Remember to put the code for decrementing in the destructors.


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
#include <iostream>

class Asteroid
{
private:
      static int numOfAsteroids;
public:
  Asteroid()
    {
      numOfAsteroids++;
      std::cout << "numOfAsteroids = " << numOfAsteroids << std::endl;
    }
  ~Asteroid()
 {
      numOfAsteroids--;
      std::cout << "numOfAsteroids = " << numOfAsteroids << std::endl;
    }
};
int Asteroid::numOfAsteroids = 0; // definition outside class declaration

  int main ()
  {
    Asteroid A;
    Asteroid B;
    return 0;
  }

Last edited on Feb 26, 2011 at 9:33pm
Feb 26, 2011 at 9:33pm
Ok, I put the increment inside my constructor:
1
2
3
4
5
6
7
8
9
10
Asteroid::Asteroid()
{
  speedX = 0.0;
  speedY = 0.0;
  angle = 0;
  posX = 0.0;
  posY = 0.0;
  size = 0;
  numOfAsteroids++;
}


but when I try to compile this is the error I get:
undefined reference to `Asteroid::numOfAsteroids'
Feb 26, 2011 at 9:38pm
This code suffers no such problem:

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
30
31
32
33
#include <iostream>

class Asteroid
{
private:
      static int numOfAsteroids;
  double speedX, speedY, angle, posX, posY, size;
public:
  Asteroid()
    {
      speedX = 0.0;
      speedY = 0.0;
      angle = 0;
      posX = 0.0;
      posY = 0.0;
      size = 0;
      numOfAsteroids++;
      std::cout << "numOfAsteroids = " << numOfAsteroids << std::endl;
    }
  ~Asteroid()
 {
      numOfAsteroids--;
      std::cout << "numOfAsteroids = " << numOfAsteroids << std::endl;
    }
};
int Asteroid::numOfAsteroids = 0; // definition outside class declaration

  int main ()
  {
    Asteroid A;
    Asteroid B;
    return 0;
  }


You must be defining/declaring Asteroid::numOfAsteroids badly.
Feb 26, 2011 at 10:04pm
ok, that works. But I don't understand why I need this line:
 
int Asteroid::numOfAsteroids = 0; // definition outside class declaration 


thanx, btw
Feb 26, 2011 at 10:10pm
You need that line because you need to initialise the class static variable to zero.
Feb 26, 2011 at 10:29pm
It has nothing to do with initialization. The static declaration inside the class is just that -- its a declaration, not an instantiation. You have to instantiate the variable somewhere.

Feb 26, 2011 at 10:35pm
Perhaps we are using different meanings of initialise in this case. When I say
initialise the class static variable to zero
I mean that it must (in this case) be initially given a value of zero if it is to be a count of the number of asteroid objects created.
Last edited on Feb 26, 2011 at 10:37pm
Feb 27, 2011 at 3:57pm
No, we're using the same meaning.

Statics are initialized to zero by default -- there is no need to make the explicit assignment as mikeglaz did. (Though it doesn't hurt -- in fact, I do it too).

The reason the line exists is for everything else but the "= 0" part. It instantiates the variable.
Feb 27, 2011 at 3:58pm
I did not know that the value would be initialised to zero by default. I stand corrected.
Topic archived. No new replies allowed.