Reference to reference?

G

Gernot Frisch

Hi,

just a stupid question:
int a(0),b(1);
int& ra = a;
int& rb = b;

ra = rb; // copies the data from b to a, right?

How would you copy the pointer to b in ra, so that ra points to b?
 
Ö

Öö Tiib

Hi,

just a stupid question:
int a(0),b(1);
int& ra = a;
int& rb = b;

ra = rb; // copies the data from b to a, right?

How would you copy the pointer to b in ra, so that ra points to b?

You wouldnt. Reference is not pointer. It is behaving very similarly
to constant pointer to (possibly non-constant) object. Try with such
pointers:

int* const pa = &a;
int* const pb = &b;
*pa = *pb; // copies the data from b to a
pa = pb; // error.
 
R

red floyd

Hi,

just a stupid question:
int a(0),b(1);
int& ra = a;
int& rb = b;

ra = rb; // copies the data from b to a, right?

How would you copy the pointer to b in ra, so that ra points to b?

Repeat after me. "A Reference is not a Pointer".


Yes, a reference *may be* implemented as a pointer under the hood,
but that's irrelevant. Also, in your example code, the compiler
probably completely optimizes out the reference, so there's no
"underlying" implementation details.

A reference cannot be reseated once initialized.
 

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

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top