Wednesday, September 1, 2010

Static data member in c++ classes

Note: Static data member are already initialised to 0, but still you should initialise explicitly using method 2.
Here are the two ways I was talking about :
First One:

class class1{




static int DataMember;

static void initFun()

{ DataMember = 10 ; }


Second One:

class class2{




static int DataMember2;


int class2::DataMember2 = 10 ;
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.

No comments:

Post a Comment