"Daqian Yang" <05*****@acadiau.ca> wrote in message
news:bv***********@poseidon.acadiau.ca...
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}
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! :-)