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

Aug 25, 2018 at 9:48pm
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;

}
Aug 25, 2018 at 10:23pm
You forgot the parentheses after the function name.

1
2
std::cout << "There are : " << myClass::getHowManyInstances() << " objects." << std::endl;
                                                           ^
Last edited on Aug 25, 2018 at 10:24pm
Topic archived. No new replies allowed.