Wednesday, December 16, 2009

Static Variable

In C,
A variable declared as static in a function is initialized once, and retains its value between function calls.
It is saved in Data section, 
The default initial value of an uninitialized static variable is zero.
If a function or global variable is declared static, it can only be accessed in that file. IT Limits the scope to that file

In C++

Static member variable have only one copy per class Unlike other member variable which have Unique copies for every Object .We cannot initialize the static class member inside of the class.We cannot even initialize the static variable inside of the header file.
in .cpp we need to initialised it like this :
classname : : var_name = 0;

Static Functions are again independent of objects .


Main feature is "this" pointer is not passed to static functions because of that
1) A static member function Can't access NON STATIC MEMBERS can only access static member data
2)A static member function cannot be declared virtual, whereas a non-static member functions can be declared as virtual
3) Can't be declared const and volatile too.

4) Doesn't need Object to invoke .However if required can be invoked through object on need basis ,

1 comment:

Followers