Function return type

This is a discussion on Function return type within the C++ Programming forums, part of the General Programming Boards category; I have just started learning c++ and I have a question. In the following example is confusing to me why ...

  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
    20,982
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 09:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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