Step 5 of 9
static members
A static data member belongs to the class, not to any one object. All objects share it. A static member function can be called without an object:
class Bot {
public:
Bot() { count_++; }
static int count() { return count_; }
private:
static inline int count_ = 0; // 'inline' lets you initialize it here
};
Bot a, b;
Bot::count(); // 2
Your turn: write a class Grenade that counts how many have ever been created (static int created()), and gives each one an int id() const in creation order starting at 1.
Previous: const member functions Next: Challenge: a Clock class