Address of a reference

A

Adrian

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.

Adrian
 
A

Adrian

I should have added, that I actually want a pointer to the original
object but only have a reference.
 
G

Gianni Mariani

Adrian said:
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 T> struct 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 :)....
 
R

Rolf Magnus

Adrian said:
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.

That's because references don't have addresses.
In your code, y will just contain the adress of the object that the
reference refers to.
 
G

Gianni Mariani

Adrian said:
I should have added, that I actually want a pointer to the original
object but only have a reference.

Taking the address of a reference will give you the address of the
object the reference is referring to.
 
A

Adrian

Taking the address of a reference will give you the address of the
object the reference is referring to.

Thanks, that what I wanted and thats what my example does - but wanted
to make sure that is what it is supposed to do :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top