The keyword used to specify that a data member is a class data member shared
by all instances of the class in C++ is static
. When a data member is
declared with the static
keyword inside a class, it means that this member
belongs to the class itself rather than to any particular object instance.
There is only one copy of this static data member shared among all objects of
the class
. In summary:
- Use
static
before a data member declaration inside a class to make it a class data member (shared by all instances). - Non-static data members belong to individual objects.
Example:
cpp
class MyClass {
public:
static int sharedValue; // static data member, shared by all objects
int individualValue; // non-static data member, unique to each object
};
This is the standard way in C++ to specify a class data member.