C++ Class Static Members
We can usestaticThe keyword is used to define class members as static. When we declare a class member as static, it means that no matter how many objects of the class are created, there is only one copy of the static member.

Static members are shared among all objects of the class. If no other initialization statements exist, all static data will be initialized to zero when the first object is created. We cannot place the initialization of static members inside the class definition, but we can do it outside the class using the scope resolution operator.::To re-declare the static variable in order to initialize it, as shown in the following example.
The following example helps to better understand the concept of static member data:
Example
When the above code is compiled and executed, it produces the following results:
Constructor called. Constructor called. Total objects: 2
static member functions
If a function member is declared as static, the function can be made independent of any particular object of the class. Static member functions can be called even when no class object exists,Static FunctionsSimply use the class name plus the scope resolution operator::then we can access it.
Static member functions can only access static member data, other static member functions, and other functions outside the class.
Static member functions have class scope; they cannot access the this pointer of the class. You can use static member functions to determine whether certain objects of the class have been created.
Differences between static member functions and ordinary member functions:
- Static member functions do not have a this pointer and can only access static members (including static member variables and static member functions).
- Ordinary member functions have a this pointer and can access any member of the class; static member functions do not have a this pointer.
The following example helps to better understand the concept of static member functions:
Example
When the above code is compiled and executed, it produces the following results:
Inital Stage Count: 0 Constructor called. Constructor called. Final Stage Count: 2other extensions