Showing posts with label static. Show all posts
Showing posts with label static. Show all posts

Tuesday, May 25, 2010

C++ Static Functions

Static member functions have a class scope and they do not have access to the 'this' pointer of the class. When a member is declared as static, a static member of class, it has only one data for the entire class even though there are many objects created for the class. The main usage of static function is when the programmer wants to have a function which is accessible even when the class is not instantiated.



static return_data_type fucntionname()//Static function defined with keyword static
{
statement1;
//Statements for execution inside static functionstatement2;
..........
..........
}

For example if a function sys1 returning nothing is to be declared as staic function it is done as follows:

static void sys1()
{
........;
.......;
}


Accessing Static Function:

A normal member function is accessed using the object and an operator called the dot member access operator. The functions declared static or static functions are accessed using only the class name and the scope resolution operator, unlike in normal member functions where these are not used.

#include
class example
{
private:
static int sum;            
//Static dataint x;
public:
example()                  
//Constructor of the class{
sum=sum+1;
x=sum;
}


~example()                //Destructor of the class{
sum=sum-1;
}


static void sys1()       //Static function exforsys( ) defined with keyword static{
cout<<"\nResult is: "<

}


void number()            //Normal member function number( ){
cout<<"\nNumber is: "<

}
};


void main()
{
example e1;
example::sys1();
//Static function exforsys() accessed using class name example and the scope resolution operator ::example e2,e3,e4;
example::sys1();
e1.number();
//Normal member function accessed using object e1 and the dot member accessoperator.
e2.number();
e3.number();
e4.number();
}



The output of the above program is:


Result is: 1
Result is: 4
Number is: 1
Number is: 2
Number is: 3
Number is: 4

The programmer must note the following while using static member functions:


  • A static member function can only access static member data, static memberfunctions and data and functions outside the class. The programmer must take note not to use static member function in the same manner as non-static member function, as non-static member function can access all of the above including thestatic data member. .
  • A non-static member function can be declared as virtual but care must be taken not to declare a static member function as virtual. .
  • The programmer must first understand the concept of static data while learning the context of static functions. It is possible to declare a data member of a class asstatic irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once. Non-static data members are created again and again. For each separate object of the class, the static data is created and initialized only once. As in the concept ofstatic data, all objects of the class in static functions share the variables. This applies to all objects of the class. .
  • A non-static member function can be called only after instantiating the class as an object. This is not the case with static member functions. A static member function can be called, even when a class is not instantiated. .
  • A static member function cannot have access to the 'this' pointer of the class.
reference exforsys.com





Wednesday, December 23, 2009

Static Variable in C++

Static Variable in C++:

You have to define the static data member outside the class declaration.

When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.

When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.

When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. A static data member must be defined at file scope. An integral data member that you declare as const static can have an initializer.

When you declare a member function in a class declaration, the static keyword specifies that the function is shared by all instances of the class. A static member function cannot access an instance member because the function does not have an implicit this pointer. To access an instance member, declare the function with a parameter that is an instance pointer or reference.



//statictest.h

class

statictest

{

public:

static int a;

void sum();

};

///////////////////////////////////////////////

//statictest.cpp

#include "statictest.h"

int statictest::a; // Defining outside the class. By default value of a=0.

void statictest::sum(){

a=0;

}

///////////////////////////////////////////////

//main.cpp

#include "statictest.h"

int main()

{

statictest test;

test.sum();

}

///////////////////////////////////////////////


#include

using namespace std;
class CMyClass {
public:
static int m_i;
};

int CMyClass::m_i = 0;
CMyClass myObject1;
CMyClass myObject2;

int main() {
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;

myObject1.m_i = 1;
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;

myObject2.m_i = 2;
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;

CMyClass::m_i = 3;
cout << myObject1.m_i << endl;
cout << myObject2.m_i << endl;
}


Output--
0
0
1
1
2
2
3
3

Static Variable in C

STATIC VARIABLE in C:

Static Variable as Local Variable in a function:

Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and
values are reinitialized. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.

Had it been global variable, it would have been available outside the scope of the function, but static variable is not available outside the scope of a function (helpful in localizing errors - as it can't be changed outside the function scope).

Static Variable as Global Variable in a File:
The Scope of the variable is limited to the file only. It cannot be used outside the file. It also retains it's value throughout the life.
It works in a same way as namespace in C++, as the same variable with the same name can be used in another file and yet will be different from the static variable in the first file.