Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

this- ----Is it needed?

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

When refering to variables within a class, you can either use this-> or just refer to the variable. Now, which is better programming practice? I seem to be using a combo of the two, so which is best? this->VARIBLE_NAME = 1; or just VARIBABLE_NAME =1; ? Which should I expect to use in a comp sci course in uni? (except they teach us bloody java, but thats besides the point :P) Cheers

Share this post


Link to post
Share on other sites
Advertisement
Generally referring to variables from within a class is done without a 'this' because it is unnecessary if you write code that doesn't use the same name for two variables. Not to say that there aren't times when you can use 'this' effectively, but I have found that generally needing to write this->variable means that some code is written poorly.

Share this post


Link to post
Share on other sites
Hi!
Almost all the time you should use just VARIABLE_NAME=1. There are some situations however where the use of "this" is needed:


class MyClass {
private:
float speed;
public:
void setSpeed( float speed ) {
this->speed = speed;
}
}



Goodbye!

Share this post


Link to post
Share on other sites
oh really? I don't fully understand what you mean by "..if you write code that doesn't use the same name for two variables". You mean overloading? So you should use this-> if you use overloading?
Quote:

means that some code is written poorly.

How so?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Generally when you I use this it's because I'm returning this or adding this to a vector or something like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
oh really? I don't fully understand what you mean by "..if you write code that doesn't use the same name for two variables". You mean overloading? So you should use this-> if you use overloading?

_Sigma, the example I've posted explains EXACTLY what cozman said. Again:
class MyClass {
private:
float speed;

public:
// The argument variable name "speed" is the same as the
// member variable "speed" of this class, so you'll need
// to use "this->speed" to differentiate between then.
// "speed = speed" makes no sense. Ok?
void setSpeed( float speed ) {
this->speed = speed;
}
};



Quote:

means that some code is written poorly.
How so?

In the example above, you can avoid using "this->" just by replacing the argument's variable name with something like "newSpeed", so the final code would be "speed = newSpeed", which is more intuitive.

Share this post


Link to post
Share on other sites
Heh, yes, I posted my reply *after* you posted, but before I refreshed the page, so I got the meaning, but I just forgot to edit my post. *oops* sorry. Regardless, thanks a ton, I think (lol) I fully understand.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
(except they teach us bloody java, but thats besides the point :P)

Don't worry, "this" appears in Java too [grin]

"this" is supposed to make the code more readable, but (to paraphrase cozman) it's sloppy.

speed = newSpeed;

is better than

this->speed = speed;


Share this post


Link to post
Share on other sites
the key word this has its uses. the first that came to mind was for overloading operators.


class MyClass {
private:
float speed;

public:
void setSpeed( float SetSpeed )
{speed = SetSpeed;}


MyClass operator= (const MyClass &ToSet)
{
speed = ToSet.speed;
return *this;
}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by poss74
Quote:
Original post by _Sigma
(except they teach us bloody java, but thats besides the point :P)

Don't worry, "this" appears in Java too [grin]


gggrreeat.....0_o

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!