Should I use `this` within a class?

前端 未结 7 1678
小蘑菇
小蘑菇 2020-12-14 07:20

Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember? What is considered better

7条回答
  •  情深已故
    2020-12-14 07:35

    This is a matter of style. Some people like the extra this-> to make it more obvious that you are accessing a class member. But if you feel it's obvious enough without it, there will be no difference in the generated code or performance.

    (Besides the case you mentioned with overlapping scopes, this-> can also be mandatory in a template when trying to name a member of a type-dependent base class.)

提交回复
热议问题