about the reference fuction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daqian Yang

    about the reference fuction

    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(DataTy pe &a){return a}

    thanks!


  • Ron Natalie

    #2
    Re: about the reference fuction


    "Daqian Yang" <052203y@acadia u.ca> wrote in message news:bvtvea$2k2 1$1@poseidon.ac adiau.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(DataTy pe &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.


    Comment

    • jeffc

      #3
      Re: about the reference fuction


      "Daqian Yang" <052203y@acadia u.ca> wrote in message
      news:bvtvea$2k2 1$1@poseidon.ac adiau.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(DataTy pe &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! :-)


      Comment

      Working...