-1

In the code, I'm reviewing, for a given class

class A {
  void foo();
  void goo();
  int member;
};

Own members and methods of this class are always accessed like this:

void A::foo() {
  this->goo();
  this->member = 5;
}

Is there any reason to use it instead of:

void A::foo() {
  goo();
  member = 5;
}
Drise
  • 4,310
  • 5
  • 41
  • 66
  • In templates it can be needed in [some situations](http://en.cppreference.com/w/cpp/language/dependent_name). Other than that, *I* consider it unnecessary verbosity that doesn't help readability and is best avoided (except when explicitly needed). – Jesper Juhl Mar 09 '18 at 19:00

2 Answers2

1

The only scenario I would (and would be required) to use this to access the members of a template base class I inherit members from.

In this case it is superfluous and just bloats the code, as well as might confuse inexperienced programmers.

  • 1
    While that's not wrong, it's not the **only** scenario it'd be required. What if you have a parameter with the same name as a member? I would say that a more generalized "only scenario" would be any case where a member (variable or function) is overshadowed or usage would be ambiguous to someone reading the code. – scohe001 Mar 09 '18 at 18:57
  • Could you provide some example of the case you're talking about? – Przemysław Czechowski Mar 09 '18 at 18:58
  • 1
    Hmmmm... I'd judge it as pretty bad style to have members and parameters with the same name. Using a name scheme avoids that. – user2328447 Mar 09 '18 at 18:59
  • @scohe001 You are of course right, I just wanted to give one example. A good naming convention ensures that you never use the same variable name for an argument and a member though. – Gabor Varga Mar 10 '18 at 10:48
-2

There is no difference. They are exact the same thing. One is explict and other implict... that's it!

Wagner Patriota
  • 5,494
  • 26
  • 49