-2

In C++ Is using

this->data

slower than

data

?

  • 2
    It wouldn't surprise me if the compiler silently converted one to the other, so that they were both equivalent. In any case, the performance difference between the two (if there is one) is almost certainly so small that it is something you should not worry about. – Robert Harvey Jul 15 '16 at 20:12
  • It is slower to type, and that's all. The compiler converts the latter to the former. – Erik Eidt Jul 15 '16 at 22:12
  • Iff they are functionally identical, there should not be any difference, at least if the code is not interpreted. It's a QoI issue which is actually a non-issue. – Deduplicator Jul 16 '16 at 9:56
4

In most cases, there won't be a noticeable difference (unless you have the obvious case with method parameters named the same as member variables). But beware of templates!

template <typename T> struct Base {
    int i;
};

template <typename T> struct Derived : public Base<T> {
    int get_i() { return i; }
};

This will cause a compilation error, unless there is a global or somehow other accessible variable named i. Why? Because there could be a later specialization of Base<T> that does not contain a declaration for i. To fix the error (by telling the compiler it should delay the lookup of i until Derived<T> is instantiated), you would have to either use this->i or Base<T>::i (or bring it into scope with a using declaration).

For more information on this topic, search for "two phase name lookup".

Sources: GCC online docs about name lookup

  • This doesn't address speed at all, since one case simply does not compile. – hoffmale Jul 15 '16 at 22:33
  • Beware of templates, indeed! Templates are the one and only place where I do use this. – David Hammen Jul 15 '16 at 22:33
1

No surprises. They are functionally identical.

In order for the compiler to locate the member, the compiler must use the hidden this parameter. Therefore there is no functional difference between accessing a member with or without using this.

The reason to use this is to disambiguate other names that may be in scope.

0

If a class has a member variable named data and one of its (non-static) member functions also has a local variable (or parameter) named data, then data is the local variable and this->data is the member variable.

class Point
{
   double x, y;
// ...
};

// Constructor with parameters named the same as the member vars
Point::Point(double x, double y)
{
   this->x = x;
   this->y = y;
}

If not, then there is no difference: data and this->data refer to the same member variable. The only reason to write the this-> is to clarify that you mean a member variable instead of a local (and even this is unnecessary if you adopt a convention like prefixing member variables with m_).

  • 1
    Blech. I hate Hungarian notation in all its forms, and that would include m_foo. I'm not going to downvote, but blech. The solution to having a data member named foo and an argument or local variable named foo is the same as going to your doctor and complaining "Doctor! It hurts when I do this!" The solution: Don't do that then. – David Hammen Jul 15 '16 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.