C++ – ny reason to use this->

c++

I am programming in C++ for many years, still I have doubt about one thing. In many places in other people code I see something like:

void Classx::memberfunction()
{
    this->doSomething();
}

If I need to import/use that code, I simply remove the this-> part, and I have never seen anything broken or having some side-effects.

void Classx::memberfunction()
{
    doSomething();
}

So, do you know of any reason to use such construct?

EDIT: Please note that I'm talking about member functions here, not variables. I understand it can be used when you want to make a distinction between a member variable and function parameter.

EDIT: apparent duplicate:
Are there any reasons not to use "this" ("Self", "Me", …)?

Best Solution

The only place where it really makes a difference is in templates in derived classes:

template<typename T>
class A {
protected:
  T x;
};

template<typename T>
class B : A<T> {
public:
  T get() {
    return this->x;
  }
};

Due to details in the name lookup in C++ compilers, it has to be made explicitly clear that x is a (inherited) member of the class, most easily done with this->x. But this is a rather esoteric case, if you don't have templated class hierarchies you don't really need to explicitly use this to access members of a class.