Jump to content
  • Advertisement
Sign in to follow this  
too_many_stars

Good form using "this" pointer?

This topic is 702 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

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.

 

Bad form/style by 2016 standards?

Performance hit?

Too verbose?

 

Thanks,

 

Mike

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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.
 
Bad form/style by 2016 standards?
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

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!