关于C++:为什么这是一个指针而不是一个引用?

Why is 'this' a pointer and not a reference?

我读了这个问题的答案C++ +利弊,并得到了这个疑问,同时阅读评论。

programmers frequently find it confusing that"this" is a pointer but not a reference. another confusion is why"hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56

That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35

I'd call the"this" thing a pretty trivial issue though. And oops, thanks for catching a few errors in my examples of undefined behavior. :) Although I don't understand what info about size has to do with anything in the first one. A pointer is simply not allowed to point outside allocated memory – jalf Dec 22 '08 at 4:18

Is this a constant poiner? – yesraaj Dec 22 '08 at 6:35

this can be constant if the method is const int getFoo() const; <- in the scope of getFoo,"this" is constant, and is therefore readonly. This prevents bugs and provides some level of guarantee to the caller that the object won't change. – Doug T. Dec 22 '08 at 16:42

you can't reassign"this". i.e you cannot do"this = &other;", because this is an rvalue. but this is of type T*, not of type T const . i.e it's a non-constant pointer. if you are in a const method, then it's a pointer to const. T const . but the pointer itself is nonconst – Johannes Schaub - litb Dec 22 '08 at 17:53

think of"this" like this: #define this (this_ + 0) where the compiler creates"this_" as a pointer to the object and makes"this" a keyword. you can't assign"this" because (this_ + 0) is an rvalue. of course that's not how it is (there is no such macro), but it can help understand it – Johannes Schaub - litb Dec 22 '08 at 17:55

我的问题是,为什么this是指针而不是引用?有什么特别的理由让它成为一个指针吗?

一些进一步的论据解释为什么this是一个参考文献是有意义的:

  • More Effective C++中的Item 1为例:当保证我们有一个有效的对象,即不是一个空值(我的解释)时,使用引用。
  • 此外,引用被认为比指针更安全(因为我们不能用杂散的指针来破坏内存)。
  • 第三,访问引用(.的语法比访问指针(->(*)的语法更好、更短。


当语言首次发展时,在真实用户的早期版本中,没有引用,只有指针。在添加运算符重载时添加了引用,因为它需要引用一致地工作。

this的一个用途是让对象获得指向自身的指针。如果是一个参考,我们就得写&this。另一方面,当我们编写一个赋值运算符时,我们必须使用return *this,这看起来比return this简单。所以如果你有一张空白的石板,你可以用任何一种方式来争论。但是C++响应于来自用户社区的反馈(如大多数成功的事物)而逐渐进化。向后兼容性的价值完全压倒了this作为引用或指针所带来的微小优势/缺点。


去派对有点晚了…直接从马的嘴里,这就是Bjarne Stroustrup必须说的(这基本上是从《C++的设计和进化》一书中重复或摘录):

Why is"this" not a reference?

Because"this" was introduced into C++ (really into C with Classes) before references were added. Also, I chose"this" to follow Simula usage, rather than the (later) Smalltalk use of"self".


C++标准声明

9.3.2/1

In the body of a nonstatic (9.3)
member function, the keyword this is a
non-lvalue expression whose value is
the address of the object for which
the function is called. The type of
this in a member function of a class X
is X*. If the member function is
declared const, the type of this is
const X*, if the member function is
declared volatile, the type of this is
volatile X*, and if the member
function is declared const volatile,
the type of this is const volatile X*.

但在其他参考文献中,发现了一些其他的东西。所以有人主动给斯特劳斯鲁先生发了一封信。下面的对话可以在这里找到。


不管我们是怎么到这里的,我认为这是一个指针而不是一个引用是幸运的,因为这有助于它"有意义",你可以删除它:

1
2
3
void A::f () {
  delete &this;
}

我认为这是一个不一定是设计的情况,C++是更好的。