c++ - 澄清 `this` 关键字

标签 c++ pointers this

抱歉,如果这是一个微不足道的问题:

实现 1:

class Foo
{
    protected: int bar;

    public: Foo(int bar)
      {
        this->bar =bar;
      }
};

实现 2:

class Foo
{
    protected: int bar;

    public: Foo(int bar)
      {
        this.bar =bar;
      }
};

实现 2 的输出:

请求“this”中的成员“x”,其指针类型为“Foo* const”(也许您打算使用“->”?)

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

最佳答案

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

关于c++ - 澄清 `this` 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13767394/

相关文章:

android - 为什么在此代码中使用this @ MainActivity?

c++ - 使用 weak_ptr 实现观察者模式

pointers - Golang返回指向接口(interface)的指针抛出错误

python - 致命的 Python 错误 : failed to get random numbers to initialize Python

c - 尝试创建动态数组,给出意想不到的结果

java - 在 Java 中移动指针而不是某种列表或队列中的对象

javascript - 当调用设置为静态方法的变量时,这是未定义的

jquery - 当源是一个类时,我可以从 rxjs observable 获取 'this' 元素吗

c++ - 如何在我的代码中使用 Matlab 引擎(用于调用 `engOpenSingleUse()` )?

c++ - boost 序列化 - 序列化不可复制但可移动的对象?