Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 629
Dollarslice Avatar asked Sep 29 '11 16:09

Dollarslice


People also ask

What is a nonstatic data member?

Non-static data members are the variables that are declared in a member specification of a class.

What is static and non static member function?

A non-static member function can be called only after instantiating the class as an object. This is not the case with static member functions. A static member function can be called, even when a class is not instantiated. A static member function cannot have access to the this pointer of the class.

What is difference between defining a non special member function?

A non-member function always appears outside of a class. The member function can appear outside of the class body (for instance, in the implementation file). But, when you do this, the member function must be qualified by the name of its class. This is to identify that that function is a member of a particular class.

What is member function and non-member function?

Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function. A member function that is not declared as static is called a nonstatic member function.


1 Answers

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.

like image 82
Pubby Avatar answered Sep 19 '22 13:09

Pubby