[c++] Use of "this" keyword in C++

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;
}

This question is related to c++ this

The answer is


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)


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


Yes. unless, there is an ambiguity.


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


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.