I was going through the C++ Primer book and I came across the following two statements:
Because references are not objects, we may not define a reference to a reference.
Because references are not objects, they don’t have addresses. Hence, we may not define a pointer to a reference.
However, when I run the following code, I don't get errors or warnings and my code runs fine:
int main(int argc, const char * argv[]) {
// insert code here...
int i, &ri = i;
int &bi = ri;
std::cout << "The address of ri is " << &ri << std::endl;
std::cout << "The address of bi is " << &bi << std::endl;
int* p = &ri;
std::cout << p << std::endl;
return 0;
}
Output:
The address of ri is 0x7fff5fbff76
The address of bi is 0x7fff5fbff76c
0x7fff5fbff76c
Any idea why this works?
Thanks.