澄清`this`关键字

aia*_*iao 0 c++ pointers this

对不起,如果这是一个微不足道的问题:

实施1:

class Foo
{
    protected: int bar;

    public: Foo(int bar)
      {
        this->bar =bar;
      }
};
Run Code Online (Sandbox Code Playgroud)

实施2:

class Foo
{
    protected: int bar;

    public: Foo(int bar)
      {
        this.bar =bar;
      }
};
Run Code Online (Sandbox Code Playgroud)

实施2的输出:

request for member ‘x’ in ‘this’, which is of pointer type ‘Foo* const’ (maybe you meant to use ‘->’ ?)

所以this是一个指针,这个问题在代码中有语法错误

Jos*_*eld 6

您要引用的问题包含用C#编写的代码示例,而不是C++.是的,在C++中,this是一个指针,必须取消引用才能访问它指向的对象的任何成员.