How do I return a temporary result object?

This is a discussion on How do I return a temporary result object? within the C++ Programming forums, part of the General Programming Boards category; I'm far from new to programming, and I know C very well, and I'm learning C++. But C++ isn't C ...

  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677

    How do I return a temporary result object?

    I'm far from new to programming, and I know C very well, and I'm learning C++. But C++ isn't C (even if it sometimes looks ever so similar), so I'm struggling a bit...

    I have spent most of today coming up with a class that implements "very long integer", as part of an excercise in a C++ book. For no particular reason, there's no "answer" to this excercise in the book, and I'm sort of flumoxed on the way that I should solve this particular problem:

    Code:
    class verylongint {
        .... 
        verylongint &operator+(const verylongint &a) {
            verylongint temp;
            .... 
            // add the contents of a and this into temp
            return temp;
        }
    };
    
    ...  // example of how this would be used 
    main() {
        verylongint a(123), b(345);
    
        cout << "a + b = " << a + b << endl;
    }
    }
    However, this is not good - we're returning the address of a local object temp, which is stored on the stack of the operator+ method. I have seen this done several times, but as far as I'm concerned, this object doesn't exist after the return. [It is inded not possible to use this method - it crashes with a exception due to accessing "invalid address"].

    So instead, I allocate a new object using "new":
    I have spent most of today coming up with a class that implements "very long integer", as part of an excercise in a C++ book. For no particular reason, there's no "answer" to this excercise in the book, and I'm sort of flumoxed on the way that I should solve this particular problem:

    Code:
    class verylongint {
        .... 
        verylongint &operator+(const verylongint &a) {
            verylongint *temp = new verylongint;
            .... 
            // add the contents of "a" and "this" into *temp
            return *temp;
        }
    };
    // same example works here...
    This works correctly, but it leaks memory, so should I decide to do some serious and lengthy calculations, I'd eventually run out of memory - not so good.

    So what is the "correct" C++ way to solve this type of problem?

    [And I'm sorry if this is an FAQ - I couldn't find anything with the search terms I used in the forum search either - but sometimes you only need ONE word cahnged to go from "nothing" to "plenty"]

    --
    Mats

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Return it as a non reference type.

    Code:
    verylongint operator+(const verylongint &a)
    {
        //create and return a temporary
    }
    Now mind you you will need to implement a copy constuctor if you have memory allocation etc in verylongint

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    BTW, a common implementation of operator+ is done in terms of operator+=. So your operator+ would look like this as a non-member function (you could keep it as a member as well):
    Code:
    verylongint operator+(const verylongint &a, const verylongint &b)
    {
        verylongint temp(a);
        temp += b;
        return temp;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Thanks to the two of you.

    Sometimes you need someone else to give you the OBVIOUS solution!

    Daved, since I haven't even implemented a += operator (yet?), I can't use that trick - but it's a good idea for if I need to do something similar - in this case, the excercise required operator+, operator* and operator<<(ostream) and that's what I've done. [For laughs I also added operator+(int v) - just using the trick to convert v into a verylongint temp first, and then just return temp +*this.]

    Adding a operator- wouldn't be too hard. operator/ would be "intersting challenge" - dividing numbers is amongs the more complex things we can do - that's why even modern processors take dozens of cycles to do that...


    --
    Mats

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Daved, since I haven't even implemented a += operator (yet?), I can't use that trick
    No worries. It's not really a trick though. It is actually better design than having separate implementations. In most cases it is not logical to have operator+ but not operator+=, so you almost always want to implement both (the obvious exception being an assignment that doesn't require both). And if you are implementing both, it is best to keep that implementation in one place to reduce bugs and maintain consistency.

    So just keep that in mind for future implementations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-21-2003, 12:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21