Connecting Tech Pros Worldwide Forums | Help | Site Map

about the reference fuction

Daqian Yang
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi all,

I have a question here, could anyone tell me what the differences bw
those funtions:

DataType move(DataType a){return a}

DataType &move(DataType &a){return a}

thanks!



Ron Natalie
Guest
 
Posts: n/a
#2: Jul 22 '05

re: about the reference fuction



"Daqian Yang" <052203y@acadiau.ca> wrote in message news:bvtvea$2k21$1@poseidon.acadiau.ca...[color=blue]
> Hi all,
>
> I have a question here, could anyone tell me what the differences bw
> those funtions:
>
> DataType move(DataType a){return a}
>
> DataType &move(DataType &a){return a}
>[/color]
The return value of the former is a copy of the value it is passed.
The return value of the latter is a reference to the same value.


jeffc
Guest
 
Posts: n/a
#3: Jul 22 '05

re: about the reference fuction



"Daqian Yang" <052203y@acadiau.ca> wrote in message
news:bvtvea$2k21$1@poseidon.acadiau.ca...[color=blue]
> Hi all,
>
> I have a question here, could anyone tell me what the differences bw
> those funtions:
>
> DataType move(DataType a){return a}
>
> DataType &move(DataType &a){return a}[/color]

The argument: in the second example it gets passed by reference instead of
value. A new copy is not made when the function is called. If it changes
in the function, it will change for the caller too (unlike the first
example).

The return type: in the second example, the return value is passed by
reference instead of value. This one is much more tricky and error prone.
It's similar in theory to using a pointer. You are not giving thecaller an
automatically created object. You are giving them a reference to some
storage that better have been dynamically created, or already existed. Then
you have to think about who is responsible for this storage after the call.
If you don't understand what I'm talking about, then don't use it until
you've learned a lot more! :-)


Closed Thread