Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about the reference fuction

Reply
Thread Tools

about the reference fuction

 
 
Daqian Yang
Guest
Posts: n/a
 
      02-05-2004
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!


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      02-05-2004

"Daqian Yang" <(E-Mail Removed)> wrote in message news:bvtvea$2k21$(E-Mail Removed)...
> 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 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.


 
Reply With Quote
 
 
 
 
jeffc
Guest
Posts: n/a
 
      02-05-2004

"Daqian Yang" <(E-Mail Removed)> wrote in message
news:bvtvea$2k21$(E-Mail Removed)...
> 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!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i used lots of static fuction is it ok? mike ASP .Net 3 11-16-2005 03:06 AM
I'd like to conver long value to IP address, which fuction do I have to use? somez72 Java 4 05-03-2004 09:27 PM
C call C++ fuction and iostream Cat C++ 2 08-20-2003 12:02 AM
An allocated variable returning by a fuction is unallocated automatically ? orion30 C++ 2 07-14-2003 10:39 AM
Re: reverse Fuction "Segmentation fault" MiniDisc_2k2 C++ 2 07-03-2003 02:52 PM



Advertisments