How to access static class data member without an object in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Animal {
public:
	static int sCount;

	Animal() { sCount++; }
	virtual ~Animal() { sCount--; }
};

int Animal::sCount = 0;

void Show()
{
	cout << "Animal::sCount = " << Animal::sCount << endl;
}

int main()
{
	Animal o1, o2;

	cout << "Animal::sCount = " << Animal::sCount << endl;

	Show();

	return 0;
}



/*
run:

Animal::sCount = 2
Animal::sCount = 2

*/

 



answered Mar 12, 2018 by avibootz

Related questions

1 answer 175 views
1 answer 192 views
1 answer 183 views
1 answer 217 views
1 answer 172 views
...