Sunday, 24 November 2013

C++ static members and function

C++ Static Members

Introduction

This article describes basic concepts of C++ Storage Access Specifiers.

Storage Access Specifiers

  • A storage class specifier is used to refine the declaration of a variable, a function, and parameters.
  • A storage class specifier do not specify the scope but combined with scope to determine the storage duration and visibility of items.
  • The storage class specifier used within the declaration determines whether:
    • The object is to be stored in memory or in a register, if available
    • The object has internal, external, or no linkage
    • The object can be referenced throughout a program or only within the function, block, or source file where the variable is defined
    • The storage duration for the object is static (storage is maintained throughout program run time) or automatic (storage is maintained only during the execution of the block where the object is defined)
  • - In C / C++ there are 4 different storage classes available: auto,extern,register,static,mutable
  • How these specifiers affect objects depend also on the scope in which they appear.
  • Storage class specifiers are keywords you can use in declarations to control storage duration and linkage.
  • The linkage determines if declaration in different scopes can refer to the same object.
  • - Storage class specifiers tell compiler the duration and visibility of the variables or objects declared, as well as, where the variables or objects should be stored.

    Static Storage Specifier

  • Class members can be declared using the storage class specifier static in the class member list
  • The declaration of a static data member in the member list of a class is not a definition. It must be defined outside the class declaration.
  • When you declare an object of a class having a static member, the static member is not part of the class object.
  • Only one copy of the static member is shared by all objects of a class in a program.
  • You can access a static member of a class by qualifying the class name using the :: (scope resolution) operator
  • Once you define a static data member, it exists even though no objects of the static data member's class exist. The progam will print value 10.
  • Static data members of a class in namespace scope have external linkage The following command shows the symbols with extern linkage and we can find the static data member in this list.The $-g$ indicates only symbols with extern linkages are displayed.We can also see that symbol is initialized in the data section which is indicated by $\mathcal{D}$

  • A static data can be initialized while defining in in namespace scope.
  • A static data member can also be initialized while declaring the class only if it is also declared as const.
  • A static data member cannot be declared as mutable
  • Local Classes cannot have static data members.
  • static data member can be of any type except void or void qualified with const or volatile.
  • Static data members and their initializers can access other static private and protected members of their classes

    Static Member function

  • You cannot have static and nonstatic member functions with the same names and the same number and type of arguments.
  • Like static data members, you may access a static member function of a class without the object of the class
  • A static member function does not have a this pointer
  • A static member function can be declared with const,volatile type qualifiers
  • A static member function cannot be declared as virtual function
  • A static member function can access only the names of static members, enumerators, and nested types of the class in which it is declared
  • A static member function cannot access the non static members of the class or base class class X{ public: int b; static int a; enum mode{a1,a2,a3}; static int inc() { return b;//this will give an error } X(){}; };
  • Static data members of class have extern linkage,they can be accessed and duration is the lifetime of the program. This can be used for class members that are required to be used in all the files and its value retained across all the files, for example logging class,monitoring class etc.
  • If a class object is defined as static,then this serves as a static storage access specifier,which defines the scope of the item to be file scope,duration is program execution and linkage is internal linkage.