Question about local variable used as a reference.
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
Re: Question about local variable used as a reference.
Quote:
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.
Re: Re: Question about local variable used as a reference.
Thanks, Kheun buddy!
George
Quote:
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.