469,950 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Reference Variable as an Alias... Huh?

X-no-archive: yes
Newbie C++ programmer reads the following statement from the C++
'Deitel & Deitel' textbook:

"Reference variables can be used as local aliases within a function.
They must be initialized upon declaration, and cannot be reassigned to
other variables thereafter. All operations supposedly performed on the
alias is actually performed on the variable."

So my question is... what the heck is the point of using a reference
variables as an alias? It seems so much simpler to use the variable
itself instead of using an alias. I don't get it.

Oct 3 '05 #1
4 12780
Using the reference variable allows you to change the value of the
variable in your function. If you don't pass it by reference, then a
copy of the variable will be created in your function (pass by value).
Answering your question, aliases are for speed and for modifying
variables in functions.

Oct 4 '05 #2
me****************@yahoo.com wrote:
X-no-archive: yes
Newbie C++ programmer reads the following statement from the C++
'Deitel & Deitel' textbook:

"Reference variables can be used as local aliases within a function.
They must be initialized upon declaration, and cannot be reassigned to
other variables thereafter. All operations supposedly performed on the
alias is actually performed on the variable."

So my question is... what the heck is the point of using a reference
variables as an alias? It seems so much simpler to use the variable
itself instead of using an alias. I don't get it.


Think about passing a reference argument to a function:

void setTo10( int& i ) { i = 10; }

If you didn't use a reference, you would be setting a *copy* of what you passed
to setTo10(). Another way to write this function would be with a pointer:

void setTo10( int* i ) { *i = 10; }
Mike
Oct 4 '05 #3
Ian
me****************@yahoo.com wrote:
X-no-archive: yes
Newbie C++ programmer reads the following statement from the C++
'Deitel & Deitel' textbook:

"Reference variables can be used as local aliases within a function.
They must be initialized upon declaration, and cannot be reassigned to
other variables thereafter. All operations supposedly performed on the
alias is actually performed on the variable."

So my question is... what the heck is the point of using a reference
variables as an alias? It seems so much simpler to use the variable
itself instead of using an alias. I don't get it.

Very handy shorthand if you want to work on a nested struct member, for
example the value part of a std::map iterator.

int& n = thing.bit.int;

or

Thing& thing = *iterator.second;
Ian
Oct 4 '05 #4
In message <11**********************@g47g2000cwa.googlegroups .com>,
si******@gmail.com writes

[no context quoted - please learn how to quote when using Google]
Using the reference variable allows you to change the value of the
variable in your function. If you don't pass it by reference, then a
copy of the variable will be created in your function (pass by value).
You're talking about passing *parameters* by reference. The OP is asking
about creating a local (automatic) reference to the result of some
expression.
Answering your question, aliases are for speed and for modifying
variables in functions.


Answering his actual question, references can be used as shorthand to
avoid repeatedly having to type a complex expression, e.g. one involving
function calls or the . -> or [] operators..

--
Richard Herring
Oct 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jan Pieter Kunst | last post: by
28 posts views Thread by William Xuuu | last post: by
51 posts views Thread by Kuku | last post: by
10 posts views Thread by ma740988 | last post: by
5 posts views Thread by pauldepstein | last post: by
11 posts views Thread by asdf | last post: by
8 posts views Thread by Bart Simpson | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.