为什么'这是一个指针,而不是一个参考? [英] Why 'this' is a pointer and not a reference?

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

问题描述

我正在阅读此问题的答案 C ++的优点和缺点/ a>,并在阅读评论时得到了这个疑问。


程序员经常发现,this是一个指针,而不是引用。另一个混乱是为什么hello不是类型std :: string但评估为一个char const *(指针)(数组到指针转换后)评论时间70年01月01日原作者:Johannes Schaub - b
$ b

这只表明它不使用与其他(以后的)语言相同的约定。评论时间70年01月01日原作者:le dorfier



我会称之为这个事情一个非常微不足道的问题。和oops,感谢在我的未定义的行为的例子中捕获一些错误。 :)虽然我不明白什么信息关于大小有关的任何东西在第一个。一个指针是不允许指向外部分配的内存 - 评论时间70年01月01日原作者:jalf这是一个常数poiner?评论时间70年01月01日原作者:yesraaj



这可以是常量,如果方法是const int getFoo()const; < - 在getFoo的范围内,this是常量,因此是只读的。这防止了错误,并为调用者提供了一定程度的保证,对象不会改变。评论时间70年01月01日原作者:Doug T.



你不能重新分配this。即你不能做this =&other;,因为这是一个右值。但是这是类型T *,而不是类型T const。即它是一个非常量指针。如果你在一个const方法,那么它是一个指针const。 T const。但是指针本身是不定的评论时间70年01月01日原作者:Johannes Schaub - litb



想象this像这样#define this(this_ +其中编译器创建this_作为指向对象的指针,并使this一个关键字。你不能分配this,因为(this_ + 0)是一个右值。当然不是怎么回事(没有这样的宏),但它可以帮助理解它 - 评论时间70年01月01日原作者:Johannes Schaub - / /

我的问题是,为什么这个是一个指针而不是引用?






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




  • 考虑项目1 code>更有效的C ++ :当保证我们有一个有效的对象,即不是一个NULL(我的解释)使用引用。

  • 此外,引用被认为比指针更安全(因为我们不能用一个离散指针来调整内存)。

  • 第三,访问引用()的语法比访问指针( - > (*))。


解决方案

当语言第一次演变时,在早期版本中,真实用户没有引用,只有指针。



的使用之一是一个对象获取一个指向自身的指针。如果它是一个引用,我们必须写& this 。另一方面,当我们写一个赋值运算符时,我们必须 return * this ,它看起来比较简单 return this 。所以,如果你有一个空白的石板,你可以争论任何一种方式。但是C ++逐渐响应来自用户社区的反馈(像最成功的事情)。向后兼容性的价值完全压倒了源自这个作为引用或指针的次要优点/缺点。


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

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

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


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

  • 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.

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.

这篇关于为什么'这是一个指针,而不是一个参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆