Top Banner

of 11

C++ static members and function

Jun 04, 2018

Download

Documents

pi194043
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 C++ static members and function

    1/11

    C++ Static Members

    Pi19404

    November 24, 2013

  • 8/13/2019 C++ static members and function

    2/11

    Contents

    Contents

    0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2 Storage Access Specifiers . . . . . . . . . . . . . . . . . . . . . . . .

    0.2.1 Static Storage Specifier . . . . . . . . . . . . . . . . . . . . 0.2.2 Static Member function . . . . . . . . . . . . . . . . . . . .

  • 8/13/2019 C++ static members and function

    3/11

    C++ Static Members

    C++ Static Members 0.1 IntroductionThis article describes basic concepts of C++ Storage Access Speci- fiers.

    0.2 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 combinedwith scope to determine the storage duration and visibility of items.

    The storage class specifier used within the declaration deter-mines 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 ismaintained throughout program run time) or automatic (storage is maintained only during the execution of theblock 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 inwhich they appear.

    Storage class specifiers are keywords you can use in declarations to control storage duration and linkage.

  • 8/13/2019 C++ static members and function

    4/11

    C++ Static Members

    The linkage determines if declaration in different scopes canrefer to the same object.

    - Storage class specifiers tell compiler the duration and visibil-

    ity of the variables or objects declared, as well as, where the variables or objects should be stored.

    0.2.1 Static Storage Specifier

    Class members can be declared using the storage class specifierstatic 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 aclass in a program.

  • 8/13/2019 C++ static members and function

    5/11

  • 8/13/2019 C++ static members and function

    6/11

    C++ Static Members

    Static data members of a class in namespace scope have externallinkage

    the following command shows the symbols with extern linkage and we can find the static data member in this list.The indi-cates only symbols with extern linkages are displayed. We can also see that symbol is initialized in the data sectionwhich is indicated by

    A static data can be initialized while defining in in namespacescope.

    A static data member can also be initialized while declaring theclass only if it is also declared as const.

    A static data member cannot be declared as mutable

  • 8/13/2019 C++ static members and function

    7/11

    C++ Static Members

    Local Classes cannot have static data members.

    static data member can be of any type except void or void quali- fied with const or volatile.

    Static data members and their initializers can access other staticprivate and protected members of their classes

  • 8/13/2019 C++ static members and function

    8/11

    C++ Static Members

    0.2.2 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 func- tion of a class without the object of the class

  • 8/13/2019 C++ static members and function

    9/11

    C++ Static Members

    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 func- tion

  • 8/13/2019 C++ static members and function

    10/11

    C++ Static Members

    A static member function can access only the names of staticmembers, enumerators, and nested types of the class in which it is declared

    A static member function cannot access the non static mem-bers of the class or base class class X public: int b; static int a; enum modea1,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 beused 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 staticstorage access specifier,which defines the scope of the item to be file scope,duration is program execution and linkage is internal linkage.

  • 8/13/2019 C++ static members and function

    11/11

    C++ Static Members