473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

"A reference is an alias"

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

Sep 10 '06 #1
5 9005
pa**********@att.net schrieb:
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.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Sep 10 '06 #2
<pa**********@att.netwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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
}

Sep 10 '06 #3

<pa**********@att.netwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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
Sep 16 '06 #4
pa**********@att.net wrote:
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++.

Sep 17 '06 #5
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jack Tanner | last post by:
I have two complex subqueries that I need to join. I suspect this problem is due to using aliases instead of table names, but I don't know how to work around it (temporary tables?). Please help. ...
5
by: Lionel | last post by:
Hello all, two quick questions: One: What is the difference between adding a reference to something in my C# project so I can use it (for example, adding a reference to "Microsft.DirectX" when...
10
by: msnews.microsoft.com | last post by:
Hi, How do I add a reference in VC++.NET? In VB.NET and VC#.NET, there's an option in "Project" menu called "Add Reference". This will add a .NET DLL reference to the current project. After I...
5
by: Trevor Andrew | last post by:
Hi There I am having some difficulty referencing Web Services. I am using Visual Studio .NET 2003, with the .NET Framework 1.1, and trying to write a VB.NET Windows Application that consumes an...
1
by: SrDhUS | last post by:
I get the following error when I try to add a web reference using Web Reference Dialog (VS .Net 2003) Error "The proxy settings on this computer are not configured correctly for web discovery."...
0
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I...
5
by: Mike Logan | last post by:
I used WSDL.exe to generate a client side web proxy for a web service, called the web service, got the results but an array returned by the web service is not in the results. However if I use...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
4
by: Neelesh Bodas | last post by:
Just wondering about exact terminology used by the standard to describe a reference. More specifically, is "reference" a type? int i = 10; // type of i is int int &ri = i; // ri is declared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.