Static Members
Every object has its own copy of each member. Two lamps have two separate on values. But sometimes you need one value that belongs to the class itself and is shared by all of its objects. For example, a ticket machine must know how many tickets it has printed so far. For this, C++ has static members.
#include <iostream>
using namespace std;
class Ticket {
public:
static inline int count = 0;
int number;
Ticket() {
count++;
number = count;
}
};
int main() {
Ticket a;
Ticket b;
Ticket c;
cout << a.number << " " << b.number << " "
<< c.number << "\n";
cout << Ticket::count << "\n";
return 0;
}
Output:
1 2 3
3
The word static makes count one variable for the whole class, not one per object. Each new ticket adds 1 to the same count and takes the result as its own number. So number is different in each object, while there is only one count.
Because count belongs to the class, you reach it with the class name: Ticket::count. You do not need any object for that.
Why inline
The word inline is what lets you give a static member its starting value inside the class. Without it, the program does not compile:
#include <iostream>
using namespace std;
class Ticket {
public:
static int count = 0;
};
int main() {
cout << Ticket::count << "\n";
return 0;
}
Output:
main.cpp:6:16: error: ISO C++ forbids in-class initialization of non-const static member ‘Ticket::count’
"ISO C++" means the C++ standard, the official rules of the language. The rules do not allow a starting value here unless you add inline. static inline works since C++17. In older code, you will see the static member declared in the class with no value, and set once outside the class with a line like int Ticket::count = 0;. Both forms do the same job.
Static methods
A method can be static too. A static method belongs to the class, so you call it with the class name, like Ticket::getCount(). This version also makes count private, so only the class can change it:
#include <iostream>
using namespace std;
class Ticket {
private:
static inline int count = 0;
public:
Ticket() {
count++;
}
static int getCount() {
return count;
}
};
int main() {
cout << Ticket::getCount() << "\n";
Ticket a;
Ticket b;
cout << Ticket::getCount() << "\n";
return 0;
}
Output:
0
2
The first call happens before any ticket exists, and it still works. A static method runs without an object, so it has no this. That means it cannot use the normal members, which belong to each object:
#include <iostream>
using namespace std;
class Ticket {
public:
int number = 0;
static int getNumber() {
return number;
}
};
int main() {
cout << Ticket::getNumber() << "\n";
return 0;
}
Output:
main.cpp: In static member function ‘static int Ticket::getNumber()’:
main.cpp:9:16: error: invalid use of member ‘Ticket::number’ in static member function
Which ticket's number should it return? There is no answer, so the compiler stops.
In the exercise, use a static member to give each visitor a number, and a static method to return how many visitors there were.