Thread: Function return type

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    Function return type

    I have just started learning c++ and I have a question.
    In the following example is confusing to me why function Add have return type Cents and why are c1 and c2 are passed by reference.

    Code:
    class Cents
    {
    private:
        int m_nCents;
     
    public:
        Cents(int nCents) { m_nCents = nCents; }
     
        int GetCents() { return m_nCents; }
    };
     
    Cents Add(Cents &c1, Cents &c2)
    {
    Cents cTemp(c1.GetCents() + c2.GetCents());
    return cTemp;
    }
    
    int main()
    {
    Cents cCents1(6);
    Cents cCents2(8);
    Cents cCentsSum = Add(cCents1, cCents2);
    std::cout << "I have " << cCentsSum.GetCents() << " cents." << std::endl;
     
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    There's no particular reason that the Add function has to return a Cents object. It could just as well have returned an int. However, the way it is used later in the program requires that it return a Cents object.

    The reason the Add function passes by reference is probably to avoid the overhead of having to create three temporary Cents objects. The way it is implemented now it only needs to recreate one. I personally would have declared it such that it like this:

    Code:
    Cents Add(const Cents &objectOne, const Cents &objectTwo)
    {
    .....
    }
    The reason is that the Add function does not need to modify the objects, but it wants a reference. The const keyword promises us that it will never modify our objects.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Zach_the_Lizard's advice is sound, and it points out another flaw: your GetCents member function is not declared const, so you cannot call it through a const Cents object/reference/pointer. The fix is to declare GetCents as const by adding the const keyword, e.g.,
    Code:
    int GetCents() const { return m_nCents; }
    Making GetCents const makes sense since GetCents() does not modify the observable state of the Cents object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM