Search code examples
c++methodsmembernon-static

What is a nonstatic member function?


I am being told that I can't use the 'this' keyword in a class function. I'm coming from c# and i'm used to this working, but the compiler tells me that it can only be used within nonstatic member functions.

D3DXVECTOR3 position;

void Position(D3DXVECTOR3 position)
{
    this.position = position;
}

Solution

  • this is a pointer containing the address of the object.

    D3DXVECTOR3 position;
    
    void YourClassNameHere::Position(D3DXVECTOR3 position)
    {
        this->position = position;
    }
    

    Should work.

    D3DXVECTOR3 position;
    
    void YourClassNameHere::Position(D3DXVECTOR3 position)
    {
        (*this).position = position;
    }
    

    Should also work.