Thursday, July 9, 2009

Static data members in c++?

In books all complicated stuff is given if any one could explain it in a easy way.

Static data members in c++?
The same variable or function will be only one copy exist.


No matter how many objects u create. All the objects uses the same variable or function.
Reply:Books are designed to complicate simple things. Then only you will buy another one.
Reply:If you want to increase the life time of a variable make it Static data member of class so that it retains it's value.


Syntex is:


static %26lt;data type%26gt; %26lt;variable%26gt;;


eg: - static int a;
Reply:Here "i" is a static data member of class X.





class X


{


public:


static int i;


};


int X::i = 0; // definition should be outside class declaration


static data member "i" exists even though no objects of the static data member's class exist





Sample code:





#include %26lt;iostream%26gt;


using namespace std;





struct X {


static const int a = 76;


};





const int X::a;





int main() {


cout %26lt;%26lt; X::a %26lt;%26lt; endl;


}
Reply:as we know that objects are the variables of class.this means, the different objects consists of the same variables and same functions(single class members) but there values may change.but,when we declare a function or a variable as static its value is common to all objects.this indicates that a static member variable is a common friend(consisting of same character) to different objects.but a variable which is not declared as static it is not a common friend to all the objects.(


it means, a firend who is having same name but the character is different for different objects.). In simple way , a static variable is a variable which have common memory allocation for different objects.
Reply:Its is extern(global) data type. only one copy of memory is created for the static member and the same memory is shared by all funtions globally in the program.





Therefore static data members retains its value between functional calls. its is initialized to 0(if int) at the time creation of memory space for that member(by the complier). no other intialization(by user) is allowed.





Normal functions can not access static data members. only static function can access static data members.





Hope this helps a little!!!
Reply:From memory....





When you make a data member of a class each instance of the class will have its own copy...





a Static data member will be shared across all instances of the class instead





So if we have Class Door with data member IsOpen and a static data member Colour and we make 4 instances of this class


Door1, Door2, Door3, Door4





Door 1-4's IsOpen data member can be changed individually.. so door 1 and 2 can be set to "open" and door 3 and 4 can be set to "closed"





The Colour static data member however will be shared across all so setting it to "Red" will make all doors red (in effect)





Far as i remember you can even access statics when there are no instances created even
Reply:If I remember right, static members are put on the stack and not the heap. Usually regular objects/members get put on the heap and their value will be wiped, after competion of the method containing the values/members. The stack can offer them to be put on the stack, whereby their values will be saved for the next enterance into the method that contains the members/vars. There are other things related to grouping of the members in memory, but I thought this might be more of what you were looking for.
Reply:The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.


class X


{


public:


static int i;


};


int X::i = 0; // definition outside class declaration


Once you define a static data member, it exists even though no objects of the static data member's class exist. In the above example, no objects of class X exist even though the static data member X::i has been defined.


Static data members of a class in namespace scope have external linkage. The initializer for a static data member is in the scope of the class declaring the member


A static data member can be of any type except for void or void qualified with const or volatile. You cannot declare a static data member as mutable.


You can only have one definition of a static member in a program. Unnamed classes, classes contained within unnamed classes, and local classes cannot have static data members.


Static data members and their initializers can access other static private and protected members of their class. The following example shows how you can initialize static members using other static members, even though these members are private:


class C {


static int i;


static int j;


static int k;


static int l;


static int m;


static int n;


static int p;


static int q;


static int r;


static int s;


static int f() { return 0; }


int a;


public:


C() { a = 0; }


};


C c;


int C::i = C::f(); // initialize with static member function


int C::j = C::i; // initialize with another static data member


int C::k = c.f(); // initialize with member function from an object


int C::l = c.j; // initialize with data member from an object


int C::s = c.a; // initialize with nonstatic data member


int C::r = 1; // initialize with a constant value


class Y : private C {} y;


int C::m = Y::f();


int C::n = Y::r;


int C::p = y.r; // error


int C::q = y.f(); // error


The initializations of C::p and C::q cause errors because y is an object of a class that is derived privately from C, and its members are not accessible to members of C.


If a static data member is of const integral or const enumeration type, you may specify a constant initializer in the static data member's declaration. This constant initializer must be an integral constant expression. Note that the constant initializer is not a definition. You still need to define the static member in an enclosing namespace. The following example demonstrates this:


#include %26lt;iostream%26gt;


using namespace std;





struct X {


static const int a = 76;


};





const int X::a;





int main() {


cout %26lt;%26lt; X::a %26lt;%26lt; endl;


}The tokens = 76 at the end of the declaration of static data member a is a constant initializer.
Reply:we assigned the variable as static than the value of the variable is changed after the call from the function of the main();
Reply:One Common Value, u want to share with all objects of the Same Class.


For This purpose can u use a normal data member. u can't


will use Static Data members for it.





An Example is like, to count the number of Objects Created by an Class.
Reply:static data are the data members which are used as the global data and in which ever the function u may change the value it will be effect to the parameter in all the functions where it is available.





the main thing is it is the variable of type single name and single memory location through out the entire program. since it maintain the single memory , where ever the changes be made to the variable will be effected in every where.


No comments:

Post a Comment