为什么“this"是指针而不是引用?

Why is #39;this#39; a pointer and not a reference?(为什么“this是指针而不是引用?)

本文介绍了为什么“this"是指针而不是引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这个问题的答案C++ 的优点和缺点 并在阅读评论时产生了这个疑问.

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

程序员经常发现,this"和this"是混淆的.是指针但不是引用.另一个困惑是为什么你好"不是 std::string 类型,但计算结果为 char const*(指针)(在数组到指针转换之后) – Johannes Schaub - litb 2008 年 12 月 22 日 1:56

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

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

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

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

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

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

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

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

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

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

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

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

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 是指针而不是引用?使它成为指针的任何特殊原因?

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

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

Some further arguments why this being a reference would make sense:

  • 考虑 More Effective C++ 中的 Item 1:当保证我们有一个有效的对象时使用引用,即不是 NULL(我的解释).
  • 此外,引用被认为比指针更安全(因为我们不能用杂散的指针搞砸内存).
  • 第三,访问引用(.)的语法比访问指针(->(*)).
  • Consider Item 1 from 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 (*)).

推荐答案

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

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.

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

One of the uses of this is 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 this being a reference or a pointer.

这篇关于为什么“this"是指针而不是引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么“this"是指针而不是引用?

基础教程推荐