Re: Problem of Pass by Reference in C++

From: Dan Cernat (dcernat_at_excite.com)
Date: 12/15/03


Date: 15 Dec 2003 08:29:34 -0800

cheung_yuklun@i-cable.com (alan) wrote in message news:<7bd5ab13.0312150130.1759ee8e@posting.google.com>...
> Hi All,
>
> I have a problem on C++ pass by reference concept.
>
> I saw some funcions which is :
>
> e.g.
> int& func(int &a)
> {
> ....
> ..
> }
>
> I understand the use of "int &a",
> but I would like to know what is the use of "int&" ? and how to use it?
> any examples?
>
> Please help.
> Best regards,
> Alan

int &a;
and
int& a;
are exactly the same.
People prefer the first because is less confusing:

int &a, b; // declares a as reference to an int and b as int

int& a, b; // does the same but someone might think that b is also a
reference to an int

note that in example above I didn't initialised the references which
is an error.

/dan



Relevant Pages