Results 1 to 3 of 3

Thread: return types [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004

    return types [RESOLVED]

    Object Object::Foo(); vs Object& Object:Foo()

    What is the difference between returning and object and returning a reference to the object? If there is a difference, when would you use each case?
    Last edited by Darkwraith; Jun 8th, 2004 at 04:42 PM.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    The first returns a copy of a local object, the second returns a reference to a (possibly) local object. You should never return a reference to a local object, since it will fall out of scope at the end of the function and you'll have a reference to a destroyed object. You can return a reference when you are returning something that will not die in the scope of the function.

    Here's an example of when you CAN return a reference:

    Code:
    class Object
    {
        private:
            // store Object pointers to delete them later.
            vector<Object*> myObjects;
        public:
            Object& Foo()
            {
               Object* o = new Object();
               myObjects.push_back(o);
            
               // since this was allocated with new, 
               // it will live past the end of this function
               // until the Object that created it goes out of scope
               // and the constructor below is called.
               return (*o);
            }
    
            ~Object()
            {
                    while(myObjects.size() != 0)
                    {
                            delete myObjects.back();
                            myObjects.pop_back();
                    }
            }
    It's common to return references in member operators, like;


    Code:
    class BigInt
    {
    public:
        // assign *this = b; return (*this);
        BigInt& operator=(const BigInt& b);
        // assign *this = *this + b; return (*this);
        BigInt& operator+=(const BigInt& b);
        // assign *this = *this - b; return (*this);
        BigInt& operator-=(const BigInt& b);
    }
    so that you can do something like

    Code:
    BigInt a, b, c;
    a = b = c; // (b = c) returns a reference to b, so that a = b.
    Last edited by sunburnt; Jun 8th, 2004 at 04:20 PM.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    thanks for clearing that up
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

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