Static data members


  • A data member of a class can be qualified as static.
  • A static member variable has certain special characteristics.
    They are:
    • It is initialized to zero when the first object of its class is created. No other initialization is permitted.
    • Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
    • It is visible only within the class, but its lifetime is the entire program.
    • Static variables are normally used to maintain values common to the entire class.
    • The type and the scope of each static member variable must be defined outside the class definition.
    • Because , the static data members are stored separately rather than as a part of an object.
    • They are also known as class variables.

    • Click here for example

      Sharing of a static data member

      Static member functions:

      A member function that is declared static has the following properties:
      • A static function can have access to only other static members (functions or variables) declared in the same class.
      • A static member function can be called using the class name(instead of its objects) as follows:
        class-name :: function-name;

      Example


  • Constructors and Destructors << Previous

    Next >> Friend Functions

    Our aim is to provide information to the knowledge seekers. 


    comments powered by Disqus


    Footer1