C++ 为什么“this”是指针而不是引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/645994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:28:01  来源:igfitidea点击:

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

c++pointersreferencethisc++-faq

提问by Naveen

I was reading the answers to this question C++ pros and consand got this doubt while reading the comments.

我正在阅读这个问题的答案C++ pros and cons并在阅读评论时产生了这个疑问。

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”是指针而不是引用会令人困惑。另一个困惑是为什么“hello”不是 std::string 类型,而是计算为 char const*(指针)(在数组到指针转换之后) – Johannes Schaub - 2008 年 12 月 22 日 1:56

这仅表明它不使用与其他(后来的)语言相同的约定。– le dorfier 2008 年 12 月 22 日 3:35

不过,我认为“这个”事情是一个非常微不足道的问题。哎呀,感谢您在我的未定义行为示例中发现了一些错误。:) 虽然我不明白关于尺寸的信息与第一个中有什么关系。指针根本不允许指向分配的内存之外 – jalf 2008 年 12 月 22 日 4:18

这是一个常数指针吗?– yesraaj 08 年 12 月 22 日 6:35

如果方法是 const int getFoo() const,则这可以是常量;<- 在 getFoo 的范围内,“this”是常量,因此是只读的。这可以防止错误并为调用者提供某种程度的保证,即对象不会更改。– 道格 T. 08 年 12 月 22 日,16:42

你不能重新分配“这个”。即你不能做“this = &other;”,因为这是一个右值。但这是 T* 类型,而不是 T const 类型。即它是一个非常量指针。如果您使用的是 const 方法,那么它就是一个指向 const 的指针。常数。但指针本身是非常量的 – Johannes Schaub - litb 2008 年 12 月 22 日 17:53

像这样考虑“this”:#define this (this_ + 0) 其中编译器创建“this_”作为指向对象的指针并使“this”成为关键字。您不能分配“this”,因为 (this_ + 0) 是一个右值。当然事实并非如此(没有这样的宏),但它可以帮助理解它 – Johannes Schaub - litb 2008 年 12 月 22 日 17:55

My question is, why is thisa pointer a not a reference? Any particular reason for making it a pointer?

我的问题是,为什么this指针不是引用?使它成为指针的任何特殊原因?



Some further arguments why thisbeing a reference would make sense:

一些进一步的论据为什么this作为参考是有意义的:

  • Consider Item 1from More Effective C++: use references when it is guaranteed that we have a valid object i.e. not a NULL (my interpretation).
  • Furthermore, references are considered safer than pointers (because we can't screw the memory up with a stray pointer).
  • Thirdly, the syntax for accessing references (.) is a little bit nicer and shorter than accessing pointers (->or (*)).
  • 考虑Item 1More Effective C++:当保证我们有一个有效的对象时使用引用,即不是 NULL(我的解释)。
  • 此外,引用被认为比指针更安全(因为我们不能用一个杂散的指针来搞砸内存)。
  • 第三,访问引用 ( .)的语法比访问指针 (->(*))更好、更短。

采纳答案by Daniel Earwicker

When the language was first evolving, in early releases with real users, there were no references, only pointers. References were added when operator overloading was added, as it requires references to work consistently.

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

One of the uses of thisis for an object to get a pointer to itself. If it was a reference, we'd have to write &this. On the other hand, when we write an assignment operator we have to return *this, which would look simpler as return this. So if you had a blank slate, you could argue it either way. But C++ evolved gradually in response to feedback from a community of users (like most successful things). The value of backward compatibility totally overwhelms the minor advantages/disadvantages stemming from thisbeing a reference or a pointer.

的用途之一this是让对象获得指向自身的指针。如果它是一个参考,我们必须写&this. 另一方面,当我们编写赋值运算符时,我们必须return *this,这看起来更简单return this。所以如果你有一张白纸,你可以用任何一种方式争论它。但是 C++ 是根据用户社区的反馈逐渐发展起来的(就像大多数成功的东西一样)。向后兼容性的价值完全压倒了this作为引用或指针的次要优点/缺点。

回答by Michael Burr

A little late to the party... Straight from the horse's mouth, here's what Bjarne Stroustrup has to say(which is essentially repeated in or taken from the "Design and Evolution of C++" book):

聚会有点晚了......直接从马嘴里说出来,这是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".

为什么“this”不是参考?

因为在添加引用之前,“this”被引入到 C++(真正引入到带有类的 C)中。另外,我选择了“this”来遵循 Simula 的用法,而不是(后来的)Smalltalk 使用“self”。