• Advertisement
Sign in to follow this  

c++ "this" pointer

This topic is 2779 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
Quote:
Original post by Aardvajk
Yuck. Code warts abound.


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


That's even better, but sometimes there just isn't a better name (for constructors or get/sets; EDIT: oh you editted ^^), if there was it means the initial definition of the member variable could've been named with the more suiting name.

Share this post


Link to post
Share on other sites
Quote:
Original post by Decrius
(for constructors or get/sets)


Well, constructors is solved, gets don't require a parameter and sets are probably better expressed as actions on the object rather than setters of members, in which case this normally resolves the issue we are discussing.

Share this post


Link to post
Share on other sites
Think to yourself: if the statements are identical, why would the compiler writers make it such that one was more expensive. If the optimisation is trivial, they probably have already included it.

In general, performance issues are solved by profiling and changing the high level algorithms in your code or writing the code to traverse data structures in a cache-friendly manner. We must assume that the compiler writers are doing a their job in managing the low level code transformation. There are exceptions to this rule (i.e. generating vectorised code), but don't worry about them for the moment.

Share this post


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

  • Advertisement