• 12
• 12
• 9
• 10
• 13

# Good form using "this" pointer?

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

## Recommended Posts

Hello everyone,

Not an altogether important question but one I have been wandering about for some time. Say we have the following example.

Point(int x, int y){
this->x=x;
this->y=y;
}


I don't see this very often, and I have heard, and read some snarky remarks with respect to the above code. Just wandering if there are any problems which I don't understand.

Performance hit?

Too verbose?

Thanks,

Mike

##### Share on other sites

This all overthinking really, but I'd prefer you rename either the incoming x and y, or the members, and get in the habit of using an initializer list.  For this simplistic example, it doesn't really matter though.

##### Share on other sites

Hello everyone,

Not an altogether important question but one I have been wandering about for some time. Say we have the following example.

Point(int x, int y){
this->x=x;
this->y=y;
}

I don't see this very often, and I have heard, and read some snarky remarks with respect to the above code. Just wandering if there are any problems which I don't understand.

Performance hit?
Too verbose?

Thanks,

Mike

If that's a constructor, I would normally write it like this:
Point(int x, int y) : x(x), y(y) {
}

I don't think there is anything wrong with this type of code.

EDIT: Oh, boy. Ninja'd by a half hour. I didn't see dmatter's post. My bad. Edited by Álvaro

##### Share on other sites
My personal take is that I _want_ it to be good form and that I find the automatic class scope lookup rules in C++ to be a mistake, but the reality is that those rules do exist and that using this-> unnecessarily is uncommon.

Coding that way won't hurt anything but it will make the code look a little alien or weird to a majority of C++ programmers, and getting in the habit of writing code _for other people_ is a good thing.

##### Share on other sites

In my experience, it's much more common to use a prefix such as m_ instead of this-> to disambiguate member variables.
I personally hate the this-> style for being too verbose... however, accessing a member variable is a performance concern when doing low level programming, and the this-> style makes this fact painfully obvious to the eye and impossible to miss, which could help you spot potential performance problems (e.g. type aliasing causing excessive loads) so that could be a positive for it. If you're not writing the kind of code where you care about whether type aliasing rules are going to cause an excessive load instruction to be generated though, then that positive isn't really applicable to you though.

##### Share on other sites

My personal take is that I _want_ it to be good form and that I find the automatic class scope lookup rules in C++ to be a mistake, but the reality is that those rules do exist and that using this-> unnecessarily is uncommon.

Coding that way won't hurt anything but it will make the code look a little alien or weird to a majority of C++ programmers, and getting in the habit of writing code _for other people_ is a good thing.

Well, you could always use something like Resharper or lint (is that still around?) to enforce that idiom?

##### Share on other sites

Thanks for all the great reply guys.

I just want to make it clear that I don't use the above code snippet, as I rarely see anything of the sort when looking at other people's source code. But once in a while I do come upon it and I was just curious that's all. The initilizer list is my preference.

I also know that many people here work/worked for professional software companies, so I wanted to know some common practices.

Thanks again,

Mike