You are on page 1of 11

C++ Static Members

Pi19404
November 24, 2013

Contents

Contents
C++ Static Members
0.1 Introduction . . . . . . . . . . . . . . . 0.2 Storage Access Specifiers . . . . 0.2.1 Static Storage Specifier 0.2.2 Static Member function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
3 3 4 8

2 | 11

C++ Static Members

C++ Static Members


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

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 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.

3 | 11

C++ Static Members

 

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.

0.2.1 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.

class X { static int f; void inc() { f=f+1; } }; int main() { X x1,x2; x1.inc(); x2.inc(); cout << x1.f << ":" << x2.f << endl; }

Only one copy of the static member is shared by all objects of a class in a program.

4 | 11

C++ Static Members

You can access a static member of a class by qualifying the class name using the :: (scope resolution) operator

class X { public: static int f; void inc1() { f=f+1; } }; int X::f=0; int main() { X x1,x2; x1.inc1(); x2.inc1(); //accessing static member cout << x1.f << ":" << x2.f << ":" << X::f << endl; }

Once you define a static data member, it exists even though no objects of the static data members class exist.

class X { public: static int fx1; void inc1() { fx1=fx1+1; } }; int X::fx1=10; int main() { //accessing static member cout << X::fx1 << endl; }
The progam will print value 10.

5 | 11

C++ Static Members

Static data members of a class in namespace scope have external linkage

nm -g --demangle a.out |grep -i fx1


the following command shows the symbols and we can find the static data member in cates only symbols with extern linkages are We can also see that symbol is initialized which is indicated by D with extern linkage this list.The g indidisplayed. in the data section

A static data can be initialized while defining in in namespace scope.

class X { public: static int fx1; void inc1() { } }; int X::fx1=1; //initializing the static data member while defining it

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

class X { public: const static int fx1=1; //declared as const static }; const int X::fx1; //defined as const nt int main() { }

A static data member cannot be declared as mutable

class X { public:

6 | 11

C++ Static Members

mutable static int fx1; void inc1() { } }; int X::fx1=1;

Local Classes cannot have static data members.

int main() { class X{ //class X is a local class static int fx1; }; int X::fx1=1;

static data member can be of any type except void or void qualified with const or volatile.

class X{ public: static void fx1() const; //this will give a erro X(){}; };

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

class X{ public: static int static int static int static int

f1; f2; f3; f4;

7 | 11

C++ Static Members

static int f5; static int f6; int f7; static int f(){return 100;} X():f7(100){}; }; X x; int X::f1=10; int X::f2=X::f1; //using f1 to initialize f2 int X::f3=X::f();//initialized using static function int X::f4=x.f1; //intialized with object data member int X::f5=x.f(); //initialized with function from object int X::f6=x.f7;//initialized using non static member of object

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.

class X{ public: static int a; static int inc() { a=a+1; } int inc() //this will give an error { } X(){}; };

Like static data members, you may access a static member function of a class without the object of the class

class X{ public: static int a; static int inc() {

8 | 11

C++ Static Members

a=a+1; } X(){}; }; int X::a=0; int main() { cerr << X::inc() << endl; }

A static member function does not have a this pointer

class X{ public: int b; static int a; static int inc() { a=this->b; //this will give an error a=a+1; } X(){}; }; X obj; int X::a=0; int main() { cerr << X::inc() << endl; }

A static member function can be declared with const,volatile type qualifiers

class X{ public: int b; static int a; const volatile static int inc() //this is allowed { a=a+1; } X(){}; };

A static member function cannot be declared as virtual function

9 | 11

C++ Static Members

class X{ public: int b; static int a; virtual static int inc() //this will given an error { a=a+1; } X(){}; };

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

class X{ public: int b; static int a; enum mode{a1,a2,a3}; static int inc() { return a1; //this is allowed since it is of enum type } X(){}; };

 

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 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 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.

10 | 11

C++ Static Members

file :a1.cpp class X{ public: int x; }; static X x1; int b() { x1.x=100; return x1.x; //returning value of member of static object } file :a2.cpp extern int b(); class X{ public: int x; }; static X x1; int main() { x1.x=101; int ff=b(); cerr << x1.x << ":" << ff << endl; //value printed is 101:100 }

11 | 11

You might also like