开发者

Is it okay to use the this pointer? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Is there any reason to use this->

When should this-> be used?

When should I make explicit use of the this pointer?

When working with pointers to classes, I like to add a this-> in front of variables in a class to make it clearer that the variable I'm talking about is in the current c开发者_StackOverflow中文版lass, as opposed to temporary variables, etc. So my lines would be something like

if(this->thing > other->thing)
    this->doFoo();

Instead of

if(thing > other->thing)
    doFoo();

Is it okay to add the superfluous this, or would that degrade code readability?


Consistency consistency consistency.

I conisder the this-> prefix a valid coding style if you use it throughout your entire project everywhere a member is accessed.

I prefer using a signifying prefix for members, e.g. m_. I feel it is less cutter and less tag soup than the explicit this->:

(alpha-this->gamma > this->alpha-gamma)

vs.

(alpha-m_gamma > m_alpha-gamma)

(The dotNetties have labeled m_ outdated - I use it on small C# projects out of spite. but anyway, any other distinct prefix would do, too.)

I've seen it used often to help intellisense get in gear, or to specifically filter members - which is ok, though leaving it in for that reason is questionable, especially if not used consistently.


That depends on your coding style, however many people would use

_myVariable
m_myVariable
myVariable_

To differentiate member variables from the other.

But the most important thing is to just be consistent


This is a style question, so answers will be subjective. Similarly, a lot of people I've worked with like to prefix member variables with m_ to make it clear that it's a member. (m_foo would be like your this->foo.) Then I'm sure there are people who feel this is a crime against the universe. YMMV. Use what works for you and anyone you might be working with.

One advantage (or disadvantage, depending on who you ask) to this-> is that you can have a variable with the same name that can be both a member and something locally scoped like a parameter or local variable, eg.:

foo bar;

void f(foo bar)
{
   this->bar = bar;
}


As already noted this is, mostly, a matter of style.

Personally I do not use it for the data-members (I use the m prefix alternative), however I do use it for functions:

  • for consistency with templated code, where this might be necessary to defer lookup
  • for clarity, in order to distinguish at a glance whether it's a method of the class (possibly a base class) or a free-standing function

I think that, since you definitely don't want to trudge through levels of base class when reading up some code, the this-> clarification makes it much easier for the reader. And it's only 6 more characters to type.


I like this pattern too, but I like it more in managed code where it's "this." - the arrow operator does feel a bit noisier, but still it makes it very clear when you're referring to instance-level stuff.


of course you can do it, besides, the compiler would add it for you.


Normally you use this notation, when your method arguments and the member variables have the same name. (to differentiate the method argument with the member variable)

Say for e.g,

void CMYClass::fun1(int sameName)
{
...
this->sameName = sameName;
}

Otherwise, it's just a matter of taste...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜