Should this operator & # 8594; to be used in the case when an internal member of the class is encountered inside the class?

My question is about this operator in C ++, should I use it as much as possible? I gave the following example to show my point:

class Abc

{
public:
  int a_;
  void fun();

};

void Abc::fun()
{
  // option 1
   a_ = 3;
  // option 2
  this->a_ = 3;
}

      

In a member of a function class, fun()

we can call member variables in two ways, one is using this->

and the other is not using it. So my question is, which practice is encouraged? Thank you.

+3


source to share


3 answers


Under normal circumstances, you are correct that you can use both. In this case, it's just a matter of style and the right thing to do while following your project's style guide. Consistency is more important than personal preference in this regard.

However, there are two situations where usage this->

can make a difference. One is when a member function has a parameter with the same name as the member; in this case, the parameter name hides the member name and you should use this->

to reference the element (first pointed out by @Krypton's answer):

void Abc::fun(int a_)
{
  a_ = 3;  // assigns into the parameter
  this->a_ = 3;  // assigns into the data member
}

      

Another situation is that you are working inside a class template and the member inherits from the base class, which depends on the template parameters of your class template. In this case, the unqualified search does not look for dependent contexts, so the member will not be found. Usage this->

turns access into a dependent expression that will be validated at instantiation time and thus correctly resolved for that element. Example:



template <class T>
struct Base
{
protected:
  T a_;
};


template <class T>
struct Abc : Base<T>
{
  void fun() {
    a_ = 3;  // error, not `a_` in scope
    this->a_ = 3;  // OK, found at instantiation time
  }
};

      

In this situation, there is an alternative solution: explicitly enter the name:

template <class T>
struct Abc : Base<T>
{
protected:
  using Base<T>::a_;

public:
  void fun() {
    a_ = 3;  // OK, found thanks to `using` declaration
  }
};

      

+4


source


If the variable and parameter are named the same, use this

is mandatory.

class Foo
{
public:
    int _a;
    void func(int _a) {
        this->_a = _a;
    }

};

      



this

also required when accessing a member of a base class that depends on the template parameter of the current class.

Otherwise, there is no need to use this

.

+4


source


I think it's more of a style issue. Using additional this->

does not change the generated code.

Although you cannot use the operator this->

in the initialization of class members before the constructor body, for example

class Abc {
public:
    Abc(int i): /*this-> is incorrect*/i(i) {}
private:
    int i;
};

      

I prefer to use this->

it to have a clear difference with other non-class members. Then code like below is more readable

void foo(int i) { }
class Abc {
public:
    Abc(int j) { this->foo(j); }
private:
    void foo (int i) { this->i = i; }
    int i;
};

      

Some people call class data members, since m_

, for example m_i

, m_j

.

Some modern IDEs support semantic syntax highlighting, which also helps distinguish between local variables, class data members, global variables, functions.

+1


source







All Articles