0

It is the second time this week I see a C++ codebase where the data members are accessed through the this pointer. For example,

class Foo
{
    int m_x;
    ...

    void bar() {
        int stuff = this->m_x;  // why not = m_x?
        ...
    }
}

You can see this in AmgX for example (Nvidia AmgX solver). What does it get us?

The only advantage I could think of is that if the function parameters happen to shadow data members, it will make it obvious which data you are using. However, I consider shadowing variables a pretty bad practice in the first place...

Anything I might have missed?

Touloudou
  • 2,079
  • 1
  • 17
  • 28
  • 3
    You reduced your example to much. The point here is that the method is a templated one. If m_x is dependent on the template, you have to use this-> to access this member – Klaus Sep 10 '19 at 14:18
  • Which shows BTW why the `m_` convention is inferior to `this->`. With `m_`, only humans know that it's a member. – MSalters Sep 10 '19 at 14:20
  • Ok that makes sense. Thanks for pointing out the other SO question. – Touloudou Sep 10 '19 at 14:26

0 Answers0