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

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

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

12条回答
  •  暖寄归人
    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; 
      }
    };
    

提交回复
热议问题