Can this pointer be used?

Possible duplicates:
Is there a reason to use this->
When should it> use?
When should I use the pointer explicitly this?

When working with pointers to classes, I would like to add this->in front of the variables in the class to make it clearer that the variable I'm talking about is in the current class, as opposed to temporary variables, etc. So my lines will be something like

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

Instead

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

Is it possible to add superfluous this, or will it impair code readability?

+3
source share
7 answers

Consistency of sequence.

this-> , , .

, . m_. , -, → :

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

.

(alpha-m_gamma > m_alpha-gamma)

(dotNetties m_ - # - , .)

, , intellisense - , , .

+4

,

_myVariable
m_myVariable
myVariable_

- .

-

+3

, . , , - m_, , . (m_foo this->foo.) , , , . YMMV. , , .

( , , ), this-> , , , , , :

foo bar;

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

, , , .

- ( m prefix), :

  • , this
  • , (, )

, , , this-> . 6 .

+2

, , "". - , , .

+1

, , , .

+1

this, - . ( -)

, ,

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

...

-1

Source: https://habr.com/ru/post/1786587/


All Articles