I read that this
is an rvalue and we cannot get its address by applying &this
.
In my code, I have tried to use a reference binding to this
. I'm wondering which way will give the address of this
? Or are both wrong?
What exactly is this
? An lvalue, an rvalue, a keyword, or something else?
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
: you're printing the address of the pointer, not the address stored in the pointerconst MyString * const& this_ref
. Taking it's address should result in the address of the pointer being referenced. The OP is trying (for utterly inexplicable reasons) to acquire&this
.this
is not unique in this regard. You also can't take the address of a variable that's markedregister
.const MyString * const& my_ref2 = this;
you'll also get a different pointer value.