81

Possible Duplicate:
Is excessive use of this in C++ a code smell
When should you use the "this" keyword in C++?
Is there any reason to use this->

In C++, is the keyword this usually omitted? For example:

Person::Person(int age) {
    _age = age;
}

As opposed to:

Person::Person(int age) {
    this->_age = age;
}
moteutsch
  • 3,741
  • 3
  • 29
  • 35

6 Answers6

110

Yes, it is not required and is usually omitted. It might be required for accessing variables after they have been overridden in the scope though:

Person::Person() {
    int age;
    this->age = 1;
}

Also, this:

Person::Person(int _age) {
    age = _age;
}

It is pretty bad style; if you need an initializer with the same name use this notation:

Person::Person(int age) : age(age) {}

More info here: https://en.cppreference.com/w/cpp/language/initializer_list

m1k1o
  • 2,344
  • 16
  • 27
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 6
    I have no idea what the last syntax is called, could you give me some hint / info about what to search for to understand how does that work ? – Stormsson Jan 07 '16 at 22:35
  • 19
    @Stormsson [Member initializer lists.](http://en.cppreference.com/w/cpp/language/initializer_list) – orlp Jan 08 '16 at 01:59
  • 1
    It's not just bad style, it's also less efficient than initialization. Moreover, const and reference variables must be initialized on the line they are declared. – Alejandro Blasco Mar 22 '18 at 09:02
  • 1
    @orlp there is actually a case where you have to use `this->` pointer: if you are using derived template classes. In the first compilation phase, the member variables of parent-classes need to be accessed with `this->` or `ParentClass::` to make sure the compiler knows that those are not typenames. – Dorian May 11 '18 at 17:12
35

It's programmer preference. Personally, I love using this since it explicitly marks the object members. Of course the _ does the same thing (only when you follow the convention)

pranavk
  • 1,774
  • 3
  • 17
  • 25
Rich
  • 12,068
  • 9
  • 62
  • 94
  • 10
    The underscore only does the same thing if you follow the convention. – R. Martinho Fernandes Jul 21 '11 at 16:44
  • 15
    Using `this` to explicitly mark object members also only works when you follow the convention. – Robert Martin Nov 13 '13 at 13:12
  • 5
    Note that "the convention" @Rich has mentioned is to use an underscore prefix to indicate object members. When you do not follow the convention, the method parameter has exactly the the same name as the object member, and `this` is required to avoid ambiguity. But I believe an explicit `this` "works" whether or not you follow the convention. – Tom Anderson Jan 05 '16 at 03:11
14

Either way works, but many places have coding standards in place that will guide the developer one way or the other. If such a policy is not in place, just follow your heart. One thing, though, it REALLY helps the readability of the code if you do use it. especially if you are not following a naming convention on class-level variable names.

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
7

this points to the object in whose member function it is reffered, so it is optional.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
5

Yes. unless, there is an ambiguity.

balki
  • 26,394
  • 30
  • 105
  • 151
  • 13
    `o avoid some compilers optimizing virtual function call`, How so? – Alok Save Jul 21 '11 at 16:47
  • I am also keen on knowing the answer to that. @AlokSave maybe you now know and are willing to share? – M.Ionut Apr 06 '20 at 13:00
  • I don't remember exactly right now. But adding 'this' was required in some special case due to some compiler bug when I wrote the answer 9 years ago. Probably it is fixed now. Removed from answer. – balki Apr 06 '20 at 16:10
3

For the example case above, it is usually omitted, yes. However, either way is syntactically correct.

Chad
  • 18,706
  • 4
  • 46
  • 63