what is static data member in c++

what is static data member in c++

1 year ago 71
Nature

A static data member in C++ is a class member that is declared using the static keyword. It has certain special characteristics, including:

  • Only one copy of the member is created for the entire class and is shared by all objects of that class, no matter how many objects are created.
  • It is initialized before any object of the class is created, even before the main function starts.
  • It is visible only within the class, but its lifetime is the entire program.

Static data members can only be defined globally in C++, with the exception of static const data members of integral type, which can be initialized in the class declaration. We can access any static member without any object by using the scope resolution operator directly with the class.

Non-static data members, on the other hand, are defined within each object created of the class. They are not shared by all objects of the class and each object has its own set of data members. Non-static data members do not need a separate definition, unlike static data members.

In summary, a static data member in C++ is a class member that is shared by all objects of the class, has a single copy, and is initialized before any object of the class is created. It is defined globally and can be accessed without any object. Non-static data members, on the other hand, are defined within each object created of the class and do not need a separate definition.

Read Entire Article