Static members (C++ only)
Class members can be declared using the storage class specifier static in the class member list. Only one copy of the static member is shared by all objects of a class in a program. When you declare an object of a class having a static member, the static member is not part of the class object.
A typical use of static members is for recording data common to all objects of a class. For example, you can use a static data member as a counter to store the number of objects of a particular class type that are created. Each time a new object is created, this static data member can be incremented to keep track of the total number of objects.
You access a static member by qualifying the class name
using the :: (scope resolution) operator. In the
following example, you can refer to the static member f() of
class type X as X::f() even if no
object of type X is ever declared:
struct X {
static int f();
};
int main() {
X::f();
}