我读到了this
是一个纯右值,因此我们不能通过应用&this
来获取它的地址。
在我的代码中,我尝试使用引用绑定到this
。我想知道哪种方法可以给出this
的地址?或者两种方式都不正确?
this
究竟是什么?是左值还是右值,是关键字还是其他东西?
void MyString::test_this() const{
std::cout << "this: " << this << std::endl;
const MyString * const& this_ref = this;
std::cout << "thie_ref: " << &this_ref << std::endl;
const MyString * const&& this_right = this;
std::cout << "thie_right: " << &this_right << std::endl;
}
//this: 00CFFC14
//thie_ref: 00CFFB14
//thie_right: 00CFFAFC
&this_ref
:你正在打印指针的地址,而不是指针中存储的地址。 - David Haimconst MyString * const& this_ref
。取它的地址应该得到被引用的指针的地址。OP正在尝试(出于无法解释的原因)获取&this
。 - WhozCraigthis
在这方面并不是唯一的。你也不能获取标记为register
的变量的地址。 - Pete Beckerconst MyString * const& my_ref2 = this;
,您将获得不同的指针值。 - aschepler