confued at using reference

J

John Black

Hi,
In the following code,

void func(int b, int h){
int dummy;
int& i1 = dummy;
int& i2 = dummy;

if ( <some condition>){
i1 = b;
i2 = h;
}
else{
i1 = h;
i2 = b;
}


< some operations>

}

I find that even I decalre i1 and i2 as 2 variable, just because
their initialization point to same variable, their addresses equal! The
statment following actually all operate on same i1/i2.

Is this as expected?

Thanks!
 
P

Phlip

John said:
Hi,
In the following code,

void func(int b, int h){
int dummy;
int& i1 = dummy;
int& i2 = dummy;

if ( <some condition>){
i1 = b;
i2 = h;
}
else{
i1 = h;
i2 = b;
}


< some operations>

}

I find that even I decalre i1 and i2 as 2 variable, just because
their initialization point to same variable, their addresses equal! The
statment following actually all operate on same i1/i2.

This is what references do.

i1 is _another name_ for dummy. It isn't another variable. The name of a
reference is almost a non-entity; it has no existence independent of its
referent.
 
J

JKop

John Black posted:
I find that even I decalre i1 and i2 as 2 variable, just because
their initialization point to same variable, their addresses equal! The
statment following actually all operate on same i1/i2.

Is this as expected?


#include <iostream>

int main(void)
{
int a;


int& c = a;

int& k = c;

a = 42;
c = 65;
k = 72;

if ( (a != 72) || (c != 72) || (k != 72) )
{
std::cout << "Burn this alleged C++ compiler";
}

if ( (&a != &c) || ( &c != &k ) || (&a != &k) )
{
std::cout << "Burn this alleged C++ compiler";
}

}

That's how references work!

-JKop
 
J

josh

John said:
In the following code,

void func(int b, int h){
int dummy;
int& i1 = dummy;
int& i2 = dummy;

if ( <some condition>){
i1 = b;
i2 = h;
}
else{
i1 = h;
i2 = b;
} [...]
I find that even I decalre i1 and i2 as 2 variable, just because
their initialization point to same variable, their addresses equal! The
statment following actually all operate on same i1/i2.

Is this as expected?

References work like pointers that are always dereferenced.

If you have a reference:
int &i1 = dummy;
you can change the definition to a pointer like so:
int * const i1 = &dummy;
and replace every other instance of i1 with (*i1) and the code will work
the same.

So your if block looks like:
if (...) {
(*i1) = b;
(*i2) = h;
}
Taking the address, &i1, becomes &(*i1).

You can't ever get the address of i1 itself (it may not have one), or
change its contents. You can never use i1 without the (*).

References have some nice optimization and reliability properties
because of this restriction.

-josh
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top