Results 1 to 7 of 7

Thread: Returning reference/value

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Returning reference/value

    I have seen return types as references (*this), and als o just regular types (thing temp; return temp). How are they diferent/when would you use each one?
    Last edited by Wynd; May 17th, 2002 at 12:45 AM.
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Actually, when someone returns a *this, it does not return a reference, because the this pointer as it's called, is already a pointer, so by saying *this, you dereference it, so you return the actual object, not just a pointer to it..

    But for your actual question, I suggest that you read: Teach Yourself C++ in 21 Days,
    Second Edition, which you can find here:
    http://newdata.box.sk/bx/c/

    The topics Pointers and References will answer your question I think...
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    jim mcnamara
    Guest
    Generally:

    return temp;

    means that temp is a value that is passed back directly to the calling function. IF temp is a char and its 'A' then 65 (Ascii A - assuming no unicode) is placed in a register and returned.
    This is used when the called function creates the value. It's called passing by value.

    return *temp;

    means that you are working with an entity external to the function - you are working with it by reference. This is used only when the object or datatype referred to is external to the function or class. This is because once the function exits, any data not directly returned to the calling program is destroyed, so passing a pointer to nothing is not useful.

  4. #4
    jim mcnamara
    Guest
    Jop - I don't believe he meant this in the strict C++ sense.
    He wanted to know the difference between by value and by reference.

    But you are correct about dereferencing.

  5. #5

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Actually, what I was talking about (and forgot to mention) was operators for classes.
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I'm sorry, but what is your actual question? operators? or return objects from classes?
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    He is talking about this:
    Code:
    MyClass& MyClass::operator = (MyClass &other)
    {
      m_a = other.m_a;
      // ...
      return *this;
    }
    Ok, let's see...

    The normal return is a return by value. jim already explained that. If the returned object is not a value but rather a complete object, the whole object is copied (which is slow).

    If you have a function that returns a large object, you might therefore want to return a reference to it to save time:
    Code:
    LARGESTRUCT & myFunc()
    {
    LARGESTRUCT ls = {213, 32, 4567, 234235, 234.35, 0x2342F, ...};
    return ls;
    }
    This does not work because the object at which the reference points is not valid anymore once the function returns. You either have to copy it, or pass a reference to such an object that is declared in the caller's scope.
    Now, with operators the arguments are clearly defined, so this doesn't work, you can't add the necessary argument and you want to have the return value. Like this:
    MyClass o1, o2, o3;
    o1 = o2 = o3 = someO;
    You need it as return value. Luckily in class members you have the this pointer, and the class object is valid even after returning because it isn't a local object. Therefore you can use it to return by reference and you don't need to make a copy. But since this is a pointer, you need to dereference it. The reult is
    return *this;
    You would write that even if the function declaration would look like this (no reference):
    MyClass operator =(MyClass &other);

    You should take a look at the syntactical differences between pointers and references.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width