When should I make explicit use of the `this` pointer?

后端 未结 12 1283
甜味超标
甜味超标 2020-11-22 15:19

When should I explicitly write this->member in a method of a class?

相关标签:
12条回答
  • 2020-11-22 15:37

    You only have to use this-> if you have a symbol with the same name in two potential namespaces. Take for example:

    class A {
    public:
       void setMyVar(int);
       void doStuff();
    
    private:
       int myVar;
    }
    
    void A::setMyVar(int myVar)
    {
      this->myVar = myVar;  // <- Interesting point in the code
    }
    
    void A::doStuff()
    {
      int myVar = ::calculateSomething();
      this->myVar = myVar; // <- Interesting point in the code
    }
    

    At the interesting points in the code, referring to myVar will refer to the local (parameter or variable) myVar. In order to access the class member also called myVar, you need to explicitly use "this->".

    0 讨论(0)
  • 2020-11-22 15:38
    1. Where a member variable would be hidden by a local variable
    2. If you just want to make it explictly clear that you are calling an instance method/variable


    Some coding standards use approach (2) as they claim it makes the code easier to read.

    Example:
    Assume MyClass has a member variable called 'count'

    void MyClass::DoSomeStuff(void)
    {
       int count = 0;
    
       .....
       count++;
       this->count = count;
    }
    
    0 讨论(0)
  • 2020-11-22 15:41

    There are several reasons why you might need to use this pointer explicitly.

    • When you want to pass a reference to your object to some function.
    • When there is a locally declared object with the same name as the member object.
    • When you're trying to access members of dependent base classes.
    • Some people prefer the notation to visually disambiguate member accesses in their code.
    0 讨论(0)
  • 2020-11-22 15:42

    You need to use this to disambiguate between a parameters/local variables and member variables.

    class Foo
    {
    protected:
      int myX;
    
    public:
      Foo(int myX)
      {
        this->myX = myX; 
      }
    };
    
    0 讨论(0)
  • 2020-11-22 15:47

    I found another interesting case of explicit usage of the "this" pointer in the Effective C++ book.

    For example, say you have a const function like

      unsigned String::length() const
    

    You don't want to calculate String's length for each call, hence you want to cache it doing something like

      unsigned String::length() const
      {
        if(!lengthInitialized)
        {
          length = strlen(data);
          lengthInitialized = 1;
        }
      }
    

    But this won't compile - you are changing the object in a const function.

    The trick to solve this requires casting this to a non-const this:

      String* const nonConstThis = (String* const) this;
    

    Then, you'll be able to do in above

      nonConstThis->lengthInitialized = 1;
    
    0 讨论(0)
  • 2020-11-22 15:51

    One other case is when invoking operators. E.g. instead of

    bool Type::operator!=(const Type& rhs)
    {
        return !operator==(rhs);
    }
    

    you can say

    bool Type::operator!=(const Type& rhs)
    {
        return !(*this == rhs);
    }
    

    Which might be more readable. Another example is the copy-and-swap:

    Type& Type::operator=(const Type& rhs)
    {
        Type temp(rhs);
        temp.swap(*this);
    }
    

    I don't know why it's not written swap(temp) but this seems to be common.

    0 讨论(0)
提交回复
热议问题