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
By the help of * (indirection operator), we can print the value of pointer variable p. Let's see the pointer example as explained for the above figure. printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.
Memory address 0 is called the null pointer. Your program is never allowed to look at or store anything into memory address 0, so the null pointer is a way of saying "a pointer to nothing". Note that a null pointer is not the same as a null character; do not confuse the two.
You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.
Yes, but it needs to have the right type. In your example int *ptr,*ptr1; both ptr and ptr1 have type "pointer to int ", which can only point to an int , not a pointer. If you declare int *ptr, **ptr1; , then ptr1 has type "pointer to int * " and thus can point to ptr . Save this answer.
I'm wondering which may give the address of
this
? Or both are wrong?
Neither is the address of this
, because the C++ abstract machine doesn't define an address for it. this
is like 0
. You can't get the address of 0, it's not an entity with storage, just some value. So what does this do?
int const& i = 0;
It creates a temporary object, initializes it with 0, and then binds the reference to it. The same exact thing occurs in your code. You create references to different temporary objects that hold the value of this
.
this
is a keyword that stands for the address of the object that the member function is being executed for. The C++ abstract machine doesn't require it to occupy storage, so it's always (logically) just a plain value, like 0.
There's merit to not requiring this
to occupy storage. It allows C++ to be implemented over an ABI where this
is passed in a register (something that isn't addressable normally). If &this
had to be well-defined, i.e. if this
had to be addressable, it would preclude an implementation from using a register for passing the address. The C++ standard generally aims not to tie implementations up like that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With