Why is my static data member and function not counting the objects correctly?

Why is my static data member and function not counting the objects correctly?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

class myClass
{
public:
myClass(int data):theDataMenber(data){howManyInstances++;}
~myClass(){howManyInstances--;}
static int getHowManyInstances(){return howManyInstances;}
private:
int theDataMenber;
static int howManyInstances;
};

int myClass::howManyInstances = 0;

int main()
{
myClass ObjectOne(5);
myClass ObjectTwo(10);
myClass ObjectThree(20);

std::cout<<"There are : " <<myClass::getHowManyInstances<< " objects."<<std::endl;

}
You forgot the parentheses after the function name.

1
2
std::cout << "There are : " << myClass::getHowManyInstances() << " objects." << std::endl;
                                                           ^
Last edited on
Topic archived. No new replies allowed.