Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   this pointer and static member functions (http://www.go4expert.com/forums/pointer-static-functions-t7971/)

faizanh 22Dec2007 12:35

this pointer and static member functions
 
Hello freinds ,
Can you please tell me why
this pointer cannot be used on static member functions.
"`this' is unavailable for static member functions"

Regards,
Faizan

shabbir 22Dec2007 14:35

Re: this pointer and static member functions
 
Syntactically
this pointer is not available in the static function because compiler does not pass the this pointer into the static functions.

As an example when you call the member function mf() from the object obj of class C as follows

obj.mf();

Then compiler internally does the following

mf(C this);

But for static function this is not the case and so its not available in the function.
Now semantically the concept is static functions can be called before the creation of object and also independent of the object and so this object is not created and so its not available.

I hope its clear now or else :shoot: more questions.

faizanh 23Dec2007 07:35

Re: this pointer and static member functions
 
Thanks for the Reply,
I just want to add STATIC FUNCTIONS and Members
dont have Class Scope, as they have static storage .

where as non static members and functions have
class scope thats why "this" pointer is available
to non static class members.

Regards,
Faizan.

Priya Dangwal 18May2012 15:30

Re: this pointer and static member functions
 
you mean Static method don't have class scope?????

Quote:

Originally Posted by faizanh (Post 22112)
Thanks for the Reply,
I just want to add STATIC FUNCTIONS and Members
dont have Class Scope, as they have static storage .

where as non static members and functions have
class scope thats why "this" pointer is available
to non static class members.

Regards,
Faizan.



All times are GMT +5.5. The time now is 04:44.