![]() |
Re: All about Static in C++
Really nice information.
|
Re: All about Static in C++
Really well explained Mridula, Not only that, what is commendable is you practically didnt leave any info about static members, and you structured it well. :2thumbsup
few things :- 1)3. We can have same named static variables in 2 different functions and are differntiated by their name that is stored in Data segment. Compler does name mangling to differentaite them. what do you mean by name mangling???? 2) 3. We cannot initialise the static members inside the class declartion. So, if we declare a class in a .h file, usually initialization is done in the respective .cc file. is it compulsory to have it in .h file? or its what people do? also is .cc and .cpp same? 3) But still static const array has to be initialized outside class as below and not in the class declaration as shown in the above example : const int Abc::count[3] = {80,90,100}; if by mistake i havent initialized it, and i try to access it, is it compile time error or it takes its default value? 4) 3. Non-Static member functions can call Static Member functions using this pointer. void Abc::display() { this->func(); //calling static function } can i do the same without using this pointer? I mean its static function belonging to the same class, so can i call it directly, since its not associated with any instance? 5) 5. Static member functions are inherited but can not be overidden. But this can be (i.e. overiding) achieved by calling a non-static virtual member function wrapper of a Derived class from a Base class pointer as below: I understood the code, how its being overiden, but i have no clue what you mean by member function wrapper of a derived class....? Thanks a lot, great post. Added Rep. |
Re: All about Static in C++
Thanks mayjune. Here I have answered to all of your queries.
1)3. We can have same named static variables in 2 different functions and are differntiated by their name that is stored in Data segment. Compler does name mangling to differentaite them. what do you mean by name mangling???? [Mridula] It is a method, used by C++ compiler to generate unique names for the identifiers. Here, since the name of static variables are same in two different function and are need to be stored in Common Data segment. So, there should be something to make them unique and thats why compiler uses name mangling. 2) 3. We cannot initialise the static members inside the class declartion. So, if we declare a class in a .h file, usually initialization is done in the respective .cc file. is it compulsory to have it in .h file? or its what people do? also is .cc and .cpp same? [Mridula] It is compulsory, that we have to initialize the static variable in .cpp file. It is because, in case if we initialize it in .h file, then there will be a compiler error stating re-definition of that static variable from the files, wherever this .h is included. 3) But still static const array has to be initialized outside class as below and not in the class declaration as shown in the above example : const int Abc::count[3] = {80,90,100}; if by mistake i havent initialized it, and i try to access it, is it compile time error or it takes its default value? [Mridula] Yes. we will get compiler error as below and does not take any default value. invalid in-class initialization of static data member of non-integral type `const int[3]' 4) 3. Non-Static member functions can call Static Member functions using this pointer. void Abc::display() { this->func(); //calling static function } can i do the same without using this pointer? I mean its static function belonging to the same class, so can i call it directly, since its not associated with any instance? [Mridula] Yes. That we can see in example 5 above. 5) 5. Static member functions are inherited but can not be overidden. But this can be (i.e. overiding) achieved by calling a non-static virtual member function wrapper of a Derived class from a Base class pointer as below: I understood the code, how its being overiden, but i have no clue what you mean by member function wrapper of a derived class....? [Mridula] In this example-5, static function "func() " is in herited in class Derived and it has it's own definition in that function "func()" right!! As per overiding, the specific function is called depending on type of the object base class pointer holds and is done using keword "virtual". But here, even though, we cannot use the keyword "virtual" for static function in Base class, but still we can have this effect of overiding by callin it from other member virtual functions. So, here, Base class pointer abc, which is holding Derived class object, calls the virtual func print of it, in trun calls it's own (Derived class's) static func() and thus achives overiding the static function "func()"!! |
Re: All about Static in C++
Nomination for article of the month - Jun 2009 Started. Nominate this article for Article of the month - Jun 2009
|
Re: All about Static in C++
Start voting for this article for Article of the month - June 2009
|
Re: All about Static in C++
This Article is winner of Article of the month - June 2009.
|
| All times are GMT +5.5. The time now is 01:44. |