Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   All about Static in C++ (http://www.go4expert.com/articles/static-cpp-t18130/)

Mridula 20Jun2009 18:26

All about Static in C++
 

Introduction



This article talks about Static in C++.

Background



Static Variables:

1. All static variables are stored in Data Segement.

2. Static variables are local in scope to their module in which they are defined, but
their life time is throughout the program.

3. All Static variables are by default intiliased by the compiler as "0", unless they are
explicitly initialised or modified with a different value.

4. Static attributes are not considered while calculating the size of a class object.

Static Global Variables:

The Global varaible declared with Static specifier is visible at the file scope. And is accessible only in the file where it is defined.

Static Local variables in a function:

1. The scope is only to that function.

2. Here, the Initialization is performed only at the first call to a function and data is
retained between calls to that function.

3. We can have same named static variables in 2 different functions and are
differntiated by their name that is stored in Data segment. Compler does name
mangling to differentaite them.

Usage of static variables in a function:

The code



Code: Cpp

int func()
{
  static int var; //initialized to 0 by default
  return (++ var);
}


Static in a class:

1. We can have Static member variables and Static member functions in a class.

2. Variables or functions do not require an instance of the class to exist.

3. One copy of this variable/function is shared between all the instances of the class.

They are normally accessed using the <class name> with scope operator ::

Static Member Variables in a class:

1. Also known as class varibles.

2. We normally access the static variables as below:
<class_name>::a ;

3. We cannot initialise the static members inside the class declartion. So, if we
declare a class in a .h file, usually initialization is done in the respective .cc file.

Syntax to initialize the static member variable is

type <class_name>::<static_variable> = value ;


Example:

The code



Code: Cpp

static4.h file
-------------

#include<iostream.h>

class Abc
{
  static int count;

  public:
    static void incr();
};

static4.cc file
---------------

#include "static4.h"

int Abc::count=0;

void Abc::incr()
{
  ++ count;
  cout<<"Static variable value:"<<Abc::count<<"\n";
}

int main()
{
  //calling static function of Abc
  Abc::incr();
  return(0);
}

Output:
Static variable value:1

Here it sounds that private varible count is accessible from outside the class, but here we are only defining and initiazing it and not really accessing it.


4. We can initialize constant static variables inside the declaration of the class

Example:

The code



Code: Cpp

#include<iostream.h>

class Abc
{
  //initializing the static variable count
  static const int count = 80;

  public:
    static void display();
};

void Abc::display()
{
  cout<<"Static constant variable value is :"<<Abc::count<<"\n";
}

int main()
{
  //calling static function of Abc
  Abc::display();
  return(0);
}

Output:

Static constant variable value is :80


But still static const array has to be initialized outside class as below and not in the class declaration as shown in the above example :

const int Abc::count[3] = {80,90,100};

Static Member Functions in a class:

1. They can only operate on static member variables.

2. A static member function does not have a this pointer.

3. Non-Static member functions can call Static Member functions using this pointer.

Example:

The code



Code: Cpp

#include<iostream.h>

class Abc
{
public:
  void display();

private:
  static void func();
};

void Abc::func()
{
  cout<<"my name is Mridula ...\n";
}

void Abc::display()
{
  this->func(); //calling static function
}

int main()
{
  Abc abc;
  abc.display();

  return (0);
}

Output:

my name is Mridula ...


4. You can call non-static member functions of a class from a Static Member function
using reference or pointers to that class object.

Example:

The code



Code: Cpp

#include<iostream.h>

class Abc
{
  public:

    Abc(){};
    ~Abc(){};
    void print();
    static void func(Abc* abc);
};

void Abc::func(Abc* abc) 
{
  abc->print();
}

void Abc::print()
{
  cout<<"My name is Mridula...\n";
}

int main()
{
  Abc *abc = new Abc;
  Abc::func(abc);
  delete (abc);

  return(0);
}

Output:

My name is Mridula...


5. Static member functions are inherited but can not be overidden.

But this can be (i.e. overiding) achieved by calling a non-static virtual member function wrapper of a Derived class from a Base class pointer as below:

The code



Code: Cpp

#include<iostream.h>

class Base
{
    static void func();

  public:

    Base(){};
    ~Base(){};
    virtual void print();
};

class Derived: public Base
{
    static void func();

  public:

    Derived(){};
    ~Derived(){};
    void print();
};

void Base::func() 
{
  cout<<"Base::My name is Mridula...\n";
}

void Base::print()
{
  func();
}

void Derived::print()
{
  func();
}

void Derived::func()
{
  cout<<"Derived::My name is Mridula...\n";
}

int main()
{
  Base *abc = new Derived;
  abc->print();
  delete (abc);

  return(0);
}

Output:

Derived::My name is Mridula...


thanks
Mridula.

shabbir 20Jun2009 21:39

Re: All about Static in C++
 
Really nice information.

mayjune 21Jun2009 03:58

Re: All about Static in C++
 
Really well explained Mridula, Not only that, what is commendable is you practically didnt leave any info about static members, and you structured it well. :2thumbsup

few things :-
1)3. We can have same named static variables in 2 different functions and are
differntiated by their name that is stored in Data segment. Compler does name
mangling to differentaite them.


what do you mean by name mangling????

2) 3. We cannot initialise the static members inside the class declartion. So, if we
declare a class in a .h file, usually initialization is done in the respective .cc file.


is it compulsory to have it in .h file? or its what people do?
also is .cc and .cpp same?

3) But still static const array has to be initialized outside class as below and not in the class declaration as shown in the above example :

const int Abc::count[3] = {80,90,100};


if by mistake i havent initialized it, and i try to access it, is it compile time error or it takes its default value?

4) 3. Non-Static member functions can call Static Member functions using this pointer.
void Abc::display()
{
this->func(); //calling static function
}


can i do the same without using this pointer? I mean its static function belonging to the same class, so can i call it directly, since its not associated with any instance?

5) 5. Static member functions are inherited but can not be overidden.

But this can be (i.e. overiding) achieved by calling a non-static virtual member function wrapper of a Derived class from a Base class pointer as below:


I understood the code, how its being overiden, but i have no clue what you mean by member function wrapper of a derived class....?

Thanks a lot, great post. Added Rep.

Mridula 22Jun2009 14:11

Re: All about Static in C++
 
Thanks mayjune. Here I have answered to all of your queries.

1)3. We can have same named static variables in 2 different functions and are
differntiated by their name that is stored in Data segment. Compler does name
mangling to differentaite them.


what do you mean by name mangling????

[Mridula] It is a method, used by C++ compiler to generate unique names for the identifiers. Here, since the name of static variables are same in two different function and are need to be stored in Common Data segment. So, there should be something to make them unique and thats why compiler uses name mangling.

2) 3. We cannot initialise the static members inside the class declartion. So, if we
declare a class in a .h file, usually initialization is done in the respective .cc file.


is it compulsory to have it in .h file? or its what people do?
also is .cc and .cpp same?


[Mridula] It is compulsory, that we have to initialize the static variable in .cpp file. It is because, in case if we initialize it in .h file, then there will be a compiler error stating re-definition of that static variable from the files, wherever this .h is included.

3) But still static const array has to be initialized outside class as below and not in the class declaration as shown in the above example :

const int Abc::count[3] = {80,90,100};


if by mistake i havent initialized it, and i try to access it, is it compile time error or it takes its default value?

[Mridula] Yes. we will get compiler error as below and does not take any default value.

invalid in-class initialization of static data member of non-integral type `const int[3]'

4) 3. Non-Static member functions can call Static Member functions using this pointer.
void Abc::display()
{
this->func(); //calling static function
}


can i do the same without using this pointer? I mean its static function belonging to the same class, so can i call it directly, since its not associated with any instance?

[Mridula] Yes. That we can see in example 5 above.

5) 5. Static member functions are inherited but can not be overidden.

But this can be (i.e. overiding) achieved by calling a non-static virtual member function wrapper of a Derived class from a Base class pointer as below:


I understood the code, how its being overiden, but i have no clue what you mean by member function wrapper of a derived class....?

[Mridula] In this example-5, static function "func() " is in herited in class Derived and
it has it's own definition in that function "func()" right!!
As per overiding, the specific function is called depending on type of the object
base class pointer holds and is done using keword "virtual".
But here, even though, we cannot use the keyword "virtual" for static function in Base class,
but still we can have this effect of overiding by callin it from other member virtual
functions.
So, here, Base class pointer abc, which is holding Derived class object, calls the virtual func print of it, in trun calls it's own (Derived class's) static func() and thus achives overiding the static function "func()"!!

shabbir 2Jul2009 11:21

Re: All about Static in C++
 
Nomination for article of the month - Jun 2009 Started. Nominate this article for Article of the month - Jun 2009

shabbir 15Jul2009 23:21

Re: All about Static in C++
 
Start voting for this article for Article of the month - June 2009

shabbir 18Jul2009 12:50

Re: All about Static in C++
 
This Article is winner of Article of the month - June 2009.


All times are GMT +5.5. The time now is 13:22.