"A reference is an alias"

P

pauldepstein

Take the code:

int ival = 1024;
int &refVal = ival;

My text says "a reference is just another name for an object ... we can
access refVal through ival ..."

I understand this way of using references.

However, it does seem to me that this is not the only way references
are used.

For example:

void swap (int &v1, int &v2)
{
int tmp = v2;
v2 = v1;
v1 = tmp;
}

I don't think v2 = v1 means "v2 refers to v1". I think v2 = v1 means:
Change the integer v2 refers to by assigning it to the same value that
v1 refers to. In other words the meaning of v2 = v1 is identical to
what it would mean if the signature was (int, int) instead of (int&,
int&).

How can I tell when a reference is acting as an alias and when it
isn't?

Paul Epstein
 
T

Thomas J. Gritzan

Take the code:

int ival = 1024;
int &refVal = ival;

This initialization sets the object, that the reference refers to. It is no
assignment.
My text says "a reference is just another name for an object ... we can
access refVal through ival ..."

I understand this way of using references.

However, it does seem to me that this is not the only way references
are used.

For example:

void swap (int &v1, int &v2)
{
int tmp = v2;
v2 = v1;
v1 = tmp;
}

I don't think v2 = v1 means "v2 refers to v1".

We also don't think that. v2 = v1 is an assignment. Assignments can't reset
references to another referee. After initialization, every assignment to a
reference affects _only_ the referenced object.
I think v2 = v1 means:
Change the integer v2 refers to by assigning it to the same value that
v1 refers to. In other words the meaning of v2 = v1 is identical to
what it would mean if the signature was (int, int) instead of (int&,
int&).

If the signature were (int, int), then v1 and v2 would be local copies of
the passed parameters, and the function had no effect to the outside.
 
J

Jim Langston

Take the code:

int ival = 1024;
int &refVal = ival;

My text says "a reference is just another name for an object ... we can
access refVal through ival ..."

I understand this way of using references.

However, it does seem to me that this is not the only way references
are used.

For example:

void swap (int &v1, int &v2)
{
int tmp = v2;
v2 = v1;
v1 = tmp;
}

I don't think v2 = v1 means "v2 refers to v1". I think v2 = v1 means:
Change the integer v2 refers to by assigning it to the same value that
v1 refers to. In other words the meaning of v2 = v1 is identical to
what it would mean if the signature was (int, int) instead of (int&,
int&).

How can I tell when a reference is acting as an alias and when it
isn't?

A reference has to be initialized. You can not say:
int& refVal;
refVal = ival;

in code. The only way, that I know of, to seat refVal if declared this way
is through a constructor initialization list.

So, *anywhere* you see
SomeRef = Something
it is an assignment, not reseating the reference, unless it is the
declaration.
I.E.
int& SomeRef = A1; // Make SomeRef refer to the varaible A1
SomeRef = A1; // Store the value in A1 into the variable refered by SomeRef

So, taking your function:
void swap (int &v1, int &v2)
// At this point v1 and v2 are seated, whatever was passed into the function

{
int tmp = v2;
// make an int and give it the value that is stored in whatever
// variable v2 is a reference to.

v2 = v1;
// assign the variable that v2 points to the value that is stored in
// the variable v1 points to.

v1 = tmp;
// assign the variable that v1 points to the value that is stored in tmp
}
 
M

Mike Wahler

Take the code:

int ival = 1024;
int &refVal = ival;

My text says "a reference is just another name for an object ... we can
access refVal through ival ..."

I understand this way of using references.

However, it does seem to me that this is not the only way references
are used.

For example:

void swap (int &v1, int &v2)
{
int tmp = v2;
v2 = v1;
v1 = tmp;
}

I don't think v2 = v1 means "v2 refers to v1". I think v2 = v1 means:
Change the integer v2 refers to by assigning it to the same value that
v1 refers to. In other words the meaning of v2 = v1 is identical to
what it would mean if the signature was (int, int) instead of (int&,
int&).

How can I tell when a reference is acting as an alias and when it
isn't?

When a reference acts as an alias: Always.
When a reference does not act as an alias: Never.

-Mike
 
O

Old Wolf

My text says "a reference is just another name for an object ... we can
access refVal through ival ..."

However, it does seem to me that this is not the only way references
are used.

For example:

void swap (int &v1, int &v2)

Referring to your quote from your text, v1 is another name for
an object in the calling function. Likewise with v2.
{
int tmp = v2;
v2 = v1;
v1 = tmp;
}

I don't think v2 = v1 means "v2 refers to v1".

Of course not. It means to assign v1 to v2, regardless of what
v2 might be. (assuming v2 is not a user-defined type with
overloaded assignment operator).
How can I tell when a reference is acting as an alias and when it
isn't?

References always act as aliases. In fact, I call them 'aliases'
whenever I can, to avoid confusion with the generic
programming term 'reference' , which covers both pointers
and references when applied to C++.
 
A

Alf P. Steinbach

* Old Wolf:
References always act as aliases. In fact, I call them 'aliases'
whenever I can, to avoid confusion with the generic
programming term 'reference' , which covers both pointers
and references when applied to C++.

Thank you. That's a very good idea. IMHO.
 

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
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top