Friday, January 18, 2008

All you have to know about static keyword

Hi! Folks

Hope this compilation on static keyword proves to be useful .
As a OOP i will try to explain static keyword w.r.t classes
A local variable is initialized when the thread of execution reaches its definition. By default, thishappens in every call of the function and each invocation of the function has its own copy of the variable. If a local variable is declared static , a single, statically allocated object will be used to represent that variable in all calls of the function. It will be initialized only the first time the thread of execution reaches its definition.


Both function and data members of a class can be made static.

A static variable provides a function with ‘‘a memory’’ without introducing a global variable that might be accessed and corrupted by other functions.

A static member can be referred to like any other member by non-static member function too. In addition, a static member can be referred to without mentioning an object.

There is exactly one copy of a s t a t i c member instead of one copy per object, as for ordinary non-st a t i c members.

Similarly, a function that needs access to members of a class, yet doesn’t need to be invoked for a particular object, is called a static member function.


When you declare a static data member within a class, you are not defining it. (That
is, you are not allocating storage for it.)

Instead, you must provide a global definition for it elsewhere, outside the class.
This is done by redeclaring the static variable using the scope resolution operator to identify the class to which it belongs.
This causes storage for the variable to be allocated. (Remember, a class declaration is simply a
logical construct that does not have physical reality.)
To understand the usage and effect of a static data member, consider this program:


#include
#include "conio.h"
using namespace std;
class X

{
int i;
static int x;
public:
X(int ii = 0) : i(ii) {} // Default
~X()
{
cout << "X::~X()" <<>
}

static void func() {//i=10;
x++;cout<
};
void f()

{
static X x1(47);
static X x2; // Default constructor required
}

int X::x =27;//initialization of static

int main()
{
f();
X::func();
getch();
}


Note:**
In C++, the preceding use of static is still supported, but deprecated. This means that it is not recommended for new code. Instead, you should use a namespace.


A static member variable exists before any object of its class is created.

One use of a static member variable is to provide access control to some shared resource used by all objects of a class.

Interesting use of a static member variable is to keep track of the number of objects of a particular class type that are in existence.

By using static member variables, you should be able to virtually eliminate any
need for global variables. The trouble with global variables relative to OOP is that they
almost always violate the principle of encapsulation.



Actually, static member functions have limited applications, but one good use
for them is to "preinitialize" private static data before any object is actually created.


What do you have to say please post your reply

Please comment on the same.
Feedbacks will be executed .

Do visit:
http://techie-paradise.nicetopic.net/index.htm
and become member for free.



Q: Can static member functions act only on static member variables?
A: Yes! It can only act on static member variables

No comments: