what exactly does the 'this' pointer does???

hiya, can anyone explain to me what exactly does the 'this' pointer does???

thanks,
grahamoj

Comments

  • : hiya, can anyone explain to me what exactly does the 'this' pointer does???
    :
    : thanks,
    : grahamoj
    :
    [code]
    class A
    {
    public:
    int m_iValue;

    A(int val) { m_iValue = val; }
    void foo ();
    };

    void A::foo ()
    {
    printf ("
    Value: %d", this->m_iValue);
    //printf ("
    Value: %d", m_iValue); //SAME RESULT!
    }

    void some_func ()
    {
    A a1 (5);
    A a2 (10);

    A* pa1 = &a1;
    A* pa2 = &a2;

    a1.foo (); // Prints: "5" ('this' inside it will be equal to 'pa1')
    a2.foo (); // Prints: "10" ('this' inside it will be equal to 'pa2')
    }
    [/code][blue]In other words - 'this' is a physical pointer to the instance (data) of the object when the member function called. In fact - every time member function is called - 'this' is passed as a hidden parameter.[/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion