# this- ----Is it needed?

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

## 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 on other sites
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 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 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 on other sites
Generally when you I use this it's because I'm returning this or adding this to a vector or something like that.

##### Share on other sites
Quote:
 Original post by _Sigmaoh 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 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 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 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 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634089
• Total Posts
3015423
×