static member funcction

Discussion in 'C++' started by Aneel, Apr 2, 2010.

  1. Aneel

    Aneel Guest

    I have confusion about 'this' pointer for static member function.
    Does the compiler pass 'this' pointer to the static member function.

    I have made a class as follows:

    class check {
    private:
    int x;
    public:
    check():x(0){}
    check(int a):x(a){}
    ~check() {}
    void callstatic() {this->print();} // here I am using 'this'
    pointer for static member function
    // and it is
    working..WHY? Since no this pointer is given to static member function
    static void print() {cout<<"STATIC";}
    };
    Aneel, Apr 2, 2010
    #1
    1. Advertising

  2. Aneel

    Ian Collins Guest

    On 04/ 2/10 06:07 PM, Aneel wrote:
    > I have confusion about 'this' pointer for static member function.
    > Does the compiler pass 'this' pointer to the static member function.


    No. Static member functions are not associated with an instance of a class.

    > I have made a class as follows:
    >
    > class check {
    > private:
    > int x;
    > public:
    > check():x(0){}
    > check(int a):x(a){}
    > ~check() {}
    > void callstatic() {this->print();} // here I am using 'this'
    > pointer for static member function


    This isn't a static member function.

    > // and it is
    > working..WHY? Since no this pointer is given to static member function
    > static void print() {cout<<"STATIC";}


    But this is.

    --
    Ian Collins
    Ian Collins, Apr 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 0to60
    Replies:
    4
    Views:
    515
    jeffc
    Nov 21, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    805
    Micah Cowan
    Jan 12, 2005
  3. aling
    Replies:
    6
    Views:
    480
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
  5. paul
    Replies:
    8
    Views:
    707
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page