473,395 Members | 1,504 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,395 software developers and data experts.

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 12932
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
28
by: William Xuuu | last post by:
Hi, References introduced make things much more complicated. e.g. int a; void f(int &n); void g(int *p); Isn't g(&a) much more clear than f(a), when we would change the value of the...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
10
by: ma740988 | last post by:
I'm perusing source that quite frankly looks like a disaster in the making. This is a case though where return by reference gets muddled with my understand based on readings. struct bar {...
5
by: pauldepstein | last post by:
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...
11
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
8
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.