Jump to content
  • Advertisement
Sign in to follow this  
dpadam450

c++ "this" pointer

This topic is 3021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know if calling "this" in a class function will optimize it into a single call or will it add the extra level of indirection:

myClass::function()
{
member_variable = 20;
}
myClass::function()
{
this->member_variable = 20;
}

Will the 2nd be slower?

Share this post


Link to post
Share on other sites
Advertisement
The statements are identical.

If 'this' is not specified, it is implied. If dereference is needed to determine the offset of address to write to, it will be performed.

Share this post


Link to post
Share on other sites
I honestly could not say. They do the exact same thing but one can add a bit more clarity in terms of reading the code. I could only assume that the compiler would make the proper optimizations and there would be no difference.

Share this post


Link to post
Share on other sites
There's no speed difference, it's only there to prevent name clash in case there's a global variable with the same name not member of your class.

Share this post


Link to post
Share on other sites
If you have VS or an IDE that has a disassembly window, create two classes as you've outlined. Set breakpoints at the 2 lines "..=20;"

Call the functions in your code and examine the disassembly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dunge
There's no speed difference, it's only there to prevent name clash in case there's a global variable with the same name not member of your class.

Hiding works the other way. In the event of a name collision, the class variable would take precedence over the global.

Share this post


Link to post
Share on other sites
As said they are identical. The reason the feature is supported is actually not to allow access to class variable when there are globals with the same name, but instead when there are parameters or locals of the same name ... see sample:


class Point2D
{
int x; int y;

void TranslateX(int x)
{
this->x += x;
}
}


the most common case of this pattern is for writing initializing constructors and setters ... so you don't have to invent stupid alternate names for your parameters / member variables ... if is far more obvious and agreeable to have:

void SetX(int x); // which sets this->x to x

than to invent names like: newX, theX, xValue, etc ...

Share this post


Link to post
Share on other sites
That, and for template classes whose inheritance depends on the template parameter. In this case, the "this->" part is not implicit for accessing the inherited stuff.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
void SetX(int x); // which sets this->x to x

than to invent names like: newX, theX, xValue, etc ...

This is actually a lot shorter than this->, and you'll never confuse them with each other:
class Point2D
{
int x;
int y;

void translate_x(int i_x)
{
x += i_x;
}
}
i for input, p for parameter, a for argument, ...

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
Quote:
Original post by Xai
void SetX(int x); // which sets this->x to x

than to invent names like: newX, theX, xValue, etc ...

This is actually a lot shorter than this->, and you'll never confuse them with each other:
class Point2D
{
int x;
int y;

void translate_x(int i_x)
{
x += i_x;
}
}
i for input, p for parameter, a for argument, ...


Yuck. Code warts abound.


void translate_x(int amount)
{
x+=amount;
}


In my experience, the only times that you frequently should be naming parameters the same as members is the constructor, in which case the initialiser list solves the problem:


class point
{
public:
point(int x,int y) : x(x),y(y) { }

int x,y;
};


Yes, there are corner cases, but they are very unusual if you think of the parameters in terms of their function rather than their nature.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!