代码之家  ›  专栏  ›  技术社区  ›  davetapley

为什么“这个”关键字不是C++中的引用类型[复制]

  •  9
  • davetapley  · 技术社区  · 15 年前

    可能的重复:
    Why ‘this’ is a pointer and not a reference?
    SAFE Pointer to a pointer (well reference to a reference) in C#

    这个 this 关键字在C++中得到指向当前对象的指针。

    我的问题是为什么 指针类型而不是引用类型。 在什么条件下 关键字将是 NULL ?

    我最近的想法是在一个静态函数中,但是VisualC++至少是足够聪明来发现和报告的。 static member functions do not have 'this' pointers . 这是标准的吗?

    3 回复  |  直到 11 年前
        1
  •  16
  •   manuelz CommonsWare    11 年前

    见Stroustrup's Why is this not a reference

    因为在添加引用之前,“这个”被引入到C++中(真正地变成C类)。另外,我选择“this”是为了遵循simula的用法,而不是(稍后)使用smalltalk的“self”。

        2
  •  3
  •   rlbond    15 年前

    因为引用直到后来才添加到C++中。到那时,改变它已经太迟了。

        3
  •  0
  •   Max    15 年前

    (可能不是完整的答案) 当一个对象需要用“危险”删除它自己时

    delete this;
    

    所以它可能是一个指针。

    M