about the reference fuction

Discussion in 'C++' started by Daqian Yang, Feb 5, 2004.

  1. Daqian Yang

    Daqian Yang Guest

    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!
    Daqian Yang, Feb 5, 2004
    #1
    1. Advertising

  2. Daqian Yang

    Ron Natalie Guest

    "Daqian Yang" <> wrote in message news:bvtvea$2k21$...
    > 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.
    Ron Natalie, Feb 5, 2004
    #2
    1. Advertising

  3. Daqian Yang

    jeffc Guest

    "Daqian Yang" <> wrote in message
    news:bvtvea$2k21$...
    > 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! :)
    jeffc, Feb 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    3
    Views:
    364
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,348
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    385
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    304
    Alf P. Steinbach
    Jul 14, 2003
  5. Cat
    Replies:
    2
    Views:
    394
    llewelly
    Aug 20, 2003
Loading...

Share This Page