Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

this- ----Is it needed?

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

Advertisement
There are times when you need to use it, for example, When you have a templated class derived from another templated class. Generally though, I only use it when I need to (eg, an object returning a reference to itself). It's just a style thing. Whenever I see this->var my brain screams at me "Extra dereference", which of course there isn't, but it's how I think at first. Apart from that, I figure I should be able to use the name directly within the body of the class functions. In my eyes, accessing everything through the this pointer just adds additional clutter, and dilutes the usefulness of syntax highlighting.

Share this post


Link to post
Share on other sites
this really just resolves scoping issues. I've never found the user or nonuse of the this keyword to be all that controversial. The only time it resolves anything is if an argument to a method is named the same as a class variable.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
When refering to variables within a class, you can either use this-> or just refer to the variable. Now, which is better programming practice?


Neither :-). Use whichever makes your code clearer, which will depend on the rest of your writing style. It's redundant if you allways prefix your class members with "m_", for example, or if you use names which make it obvious that the variable in question is a member of the class.

There is also one occasion (in reply to the title of this thread) when you must use "this->", it is outlined in this post by Fruny in a very recently posted thread titled using "this".

Share this post


Link to post
Share on other sites
Well, it really depends on how you name your variables.. I have gotten used to prefixes for member variables and function arguments, no more clashes with the names and it is immediately clear where a variable comes from. Yeah, I guess that makes it less readable for people not used to it, but it works well enough for me.
I certainly think it's better to do it this way than naming stuff "NewValue" just in those places where you need it.

Using this-> all the time just screams "C" at me.. I don't want to be reminded that technically each class method is passed a self-pointer, so I don't want to have that in my code either.

Share this post


Link to post
Share on other sites
this-> brings up an Intellisense box, so I like it for that reason.

Not using this is entirely acceptable... but I'd recommend naming your class variables with a common convention, such as starting with m_ or even just _.

Share this post


Link to post
Share on other sites
I like to use it for code clarity too. The habit I've gotten into is, whenever a child class calls a function that's defined by the parent (that it doesn't override), or uses a variable that's on the parent, I tack a this-> in front so that no one gets confused as to what's going on. So for me I guess it serves the same purpose as a comment. But it's shorter than a comment. :D

Obviously using this-> on everything is silly. I'm a big fan of the m_ naming style.

Share this post


Link to post
Share on other sites
Cname? So you just put a capital C in front of the name for member variables? I don't know, it seems like m_ is more readable. What if you want to name a variable "rap"?

Share this post


Link to post
Share on other sites
Quote:
Original post by PnP Bios
I prefer it! I use it for clarity, and to trick intellisense into showing me the members of my class.


Does that mean you don't know what members your class has? ;)

Cheers,
Drag0n

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!