this pointer and static member functions

faizanh's Avatar, Join Date: Dec 2007
Newbie Member
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's Avatar, Join Date: Jul 2004
Go4Expert Founder
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 more questions.
faizanh's Avatar, Join Date: Dec 2007
Newbie Member
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's Avatar, Join Date: May 2012
Newbie Member
you mean Static method don't have class scope?????

Quote:
Originally Posted by faizanh View Post
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.