Adrian wrote:
Is this ok?
#include <iostream>
int main(int argc, char *argv[])
{
int x=1234;
int *x_ptr=&x;
int &x_ref=x;
std::cout << std::hex << x_ptr << "=" << *x_ptr << std::endl;
int *y=&x_ref;
std::cout << std::hex << y << "=" << *y << std::endl;
return 0;
}
Cant find anything in my standard about taking addresses of
references.
You can't take the address of a reference. A reference is like an alias
for the object it is referencing.
You can "contain" a reference by placing it in a class; e.g.
template <typename Tstruct Ref
{
T & v;
Ref( T & iv ) : v( iv ) {}
};
int main()
{
int x=1234;
Ref<int refx( x );
}
however, it's somewhat useless because you can't reseat a reference
after it has been initialized (unlike a pointer).
The truly nasty hack you can to to reseat a reference is to reconstruct
the class in place (as I said - a major bad).
int main()
{
int x=1234;
Ref<int refx( x );
int y=444;
refx.~Ref<int>();
new (&refx) Ref<int>( y );
}
(I'm not sure it's compilable but you get the idea)
It is perfectly legitimate C++ code but I think you will be ridiculed if
you use it like I will be for pointing it out :-)....