Reference Variable as an Alias... Huh?

  • Thread starter mexican_equivalent
  • Start date
M

mexican_equivalent

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.
 
S

siryuhan

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.
 
M

Mike Austin

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
 
I

Ian

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
 
R

Richard Herring

In message <[email protected]>,
(e-mail address removed) 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..
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top