Static Member Functions

Static member functions are like static member variables: They exist not in an object but in the scope of the class. Therefore, they can be called without having an object of that class, as illustrated in Listing 20.2.

Listing 20.2. Static Member Functions
 0: //Listing 20.2 static data members 1: #include <iostream> 2: 3: class Cat 4: { 5: public: 6: Cat(int age = 1):itsAge(age){HowManyCats++; } 7: virtual ~Cat() { HowManyCats--; } 8: virtual int GetAge() { return itsAge; } 9: virtual void SetAge(int age) { itsAge = age; } 10: static int GetHowMany() { return HowManyCats; } 11: private: 12: int itsAge; 13: static int HowManyCats; 14: }; 15: 16: int Cat::HowManyCats = 0; 17: 18: void TelepathicFunction(); 19: 20: int ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.