1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

this pointer and static member functions

Discussion in 'C' started by faizanh, Dec 22, 2007.

  1. faizanh

    faizanh New Member

    Joined:
    Dec 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  3. faizanh

    faizanh New Member

    Joined:
    Dec 14, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. Priya Dangwal

    Priya Dangwal New Member

    Joined:
    May 18, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    New Delhi
    you mean Static method don't have class scope?????

     

Share This Page