Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use `this` within a class?

Tags:

c++

class

this

Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember? What is considered better style? Is there any performance difference?

(I am not talking about the case where a local variable has the same name as the data member, in which case you must, to my knowledge, use this-> to distinguish between them.)

like image 322
Ben Avatar asked Mar 06 '12 19:03

Ben


People also ask

Why do we use this in class?

The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

Is this necessary in Java?

In answer to "Are there any cases where you should always use this ?" You should use it when it is needed to avoid ambiguity, for example if there is another variable with the same name in scope.

Should we use this in C#?

The “this” keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between the method parameters and class fields if they both have the same name. Another usage of “this” keyword is to call another constructor from a constructor in the same class.

Should I put this -> C++?

This is a matter of style. Some people like the extra this-> to make it more obvious that you are accessing a class member. But if you feel it's obvious enough without it, there will be no difference in the generated code or performance.


2 Answers

As a general rule, it's a question of local conventions. Most of the places I've seen do not use this-> except when necessary, and that's the convention I prefer as well, but I've heard of people who prefer to use it systematically.

There are two cases when it is necessary. The first is if you've hidden the name with the same name in local scope; if e.g. you have a member named toto, and you also named your function argument toto. Many coding conventions mark either the member or argments to avoid this case, e.g. all member names start with my or m_, or a parameter name will start with the.

The other case is that this-> can be used in a template to make a name dependent. This is relevant if a template class inherits from a dependent type, and you want to access a member of the base, e.g.:

template <typename T> class Toto : public T { public:     int f()     {         return this->g();     } }; 

Without the this-> here, g() would be a non-dependent name, and the compiler would look it up in the context of the template definition, without taking the base class into consideration.

like image 92
James Kanze Avatar answered Sep 28 '22 17:09

James Kanze


I always use this when calling member functions.

  1. It turns the function name into a dependent name so that base class member functions are found within a class template.
  2. It suppresses argument-dependent lookup. ADL has its advantages, but it can lead to surprising behavior, and I like it if it's not getting in my way.
  3. It has no real disadvantages, and so I use it for all member function calls for consistency reasons.
  4. I program in Python a lot where an explicit self is mandatory, so it's not a real burden for me.

But for data members I use it only when necessary because there is no ADL taking place. To answer your specific questions:

Within a member function of a class in C++, does it make a difference, if I use this->dataMember or just dataMember?

Yes, if this is within a class template. Then dataMember is considered a non-dependent name, which can lead to semantic differences. For example:

#include <iostream>  int i = 1;  struct R {   int i;   R(): i(2) { } };  template<typename T> struct S: T {   void f() {     std::cout << i << ' '     // selects ::i               << this->i      // selects R::i               << std::endl;   } };  int main() {   S<R>().f(); } 

What is considered better style?

I don't think that there is a strong opinion within the community about this. Use either style, but be consistent.

Is there any performance difference?

I'm pretty sure there isn't.

like image 43
Philipp Avatar answered Sep 28 '22 15:09

Philipp