Click to See Complete Forum and Search --> : Question about local variable used as a reference.


George2
April 21st, 2003, 03:25 AM
Hi, everyone!


I have only noticed that reference variable are used as
parameter of a function before (or return value). For
example,


Source Code:
--------
function (const T& t)
--------


But today when reading the source code of some other's
today. I noticed that a local variable is declared as
reference type. Before I only noticed that lcoal variable
are used as pointer to origin type and origin type.

Source Code:
--------
T *pt;
T t;
--------

But now I noticed that a variable is declard like this in a
function,


Source Code:
--------
T t1;
T & t2 = t1;
--------


I want to know if a reference is used as a local variable, are there
any special things to notice? Or simply just the same as the reference
variable of a function parameter?


Thanks in advance,
George

Kheun
April 21st, 2003, 05:13 AM
Originally posted by George2
Source Code:
--------
T t1;
T & t2 = t1;
--------
Yes, you are right. Declaring a reference as a local variable has the same effect as a reference variable of a function parameter. It goes out-of-scope when the function ends.

Also after the reference has been initialized, you can't re-reference it to a different variable.

George2
April 21st, 2003, 07:34 AM
Thanks, Kheun buddy!

George

Originally posted by Kheun

Yes, you are right. Declaring a reference as a local variable has the same effect as a reference variable of a function parameter. It goes out-of-scope when the function ends.

Also after the reference has been initialized, you can't re-reference it to a different variable.

KevinHall
April 21st, 2003, 11:03 AM
Also after the reference has been initialized, you can't re-reference it to a different variable.

That's not true. t2 in your example can be reassigned to reference different instances of T as often as you like. You need a different declaration to prevent t2 fro being reassined:

T& const t3 = t1;

t3 is a variable that can only be assigned once.

- Kevin

P.S. There is a FAQ related to this. (http://www.codeguru.com/forum/showthread.php?s=&threadid=231042)

Bassman
April 21st, 2003, 11:32 AM
t2 in your example can be reassigned to reference different instances of T as often as you like.


No, it can't. An object reference cannot be changed to reference another object.


T t1, t4;
T & t2 = t1; // assigns the reference(alias) 't2' for 't1'
t2 = t4; // invokes the assignment operator for t1


The const declaration you provided prevents modification of t1 through t3.

http://www.parashift.com/c++-faq-lite/references.html

See especially section 8.5.

Regards,
Bassman

Graham
April 21st, 2003, 11:42 AM
Bassman's correct - you cannot reseat references. Also, the following is illegal:

T t1;
T& t2; // Illegal - reference must be initialised

t2 = t1; // Also illegal, can't reseat a reference
// (or does it mean call T::operator=()?)

KevinHall
April 21st, 2003, 12:15 PM
Interesting.... I can't believe I have misundestood this for so long!

Thanks for clearing this up for me,

Kevin

KevinHall
April 21st, 2003, 12:25 PM
(now deeply embarrased...) I should have realized that earlier -- much of the code I worked with and written uses that fact that operator=() gets called. As soon as I typed an example, I realized how wrong I was. I don't know why I held on to that belief. Oh well.

- Kevin

George2
April 22nd, 2003, 07:52 AM
Yes I agree, KevinHall buddy!

When sit down and think the very basic idea of
C++ prgramming. I also find myself a child of this
area even finished several projects.


regards,
George

Originally posted by KevinHall
(now deeply embarrased...) I should have realized that earlier -- much of the code I worked with and written uses that fact that operator=() gets called. As soon as I typed an example, I realized how wrong I was. I don't know why I held on to that belief. Oh well.

- Kevin