9.2a — Overloading operators using normal functions

In the previous lesson, we overloaded operator+ as a friend function:

Using a friend function to overload an operator is convenient because it gives you direct access to the internal members of the classes you’re operating on. In the initial Cents example above, our friend function version of operator+ accessed member variable m_cents directly.

However, if you don’t need that access, you can write your overloaded operators as normal functions. Note that the Cents class above contains an access function (getCents()) that allows us to get at m_cents without having to have direct access to private members. Because of this, we can write our overloaded operator+ as a non-friend:

Because the normal and friend functions work almost identically (they just have different levels of access to private members), we’ll generally won’t differentiate them. The one difference is that with the friend function, the friend function declaration inside the class also serves as a prototype. With the normal function version, you’ll have to provide your own function prototype.




In general, a normal function should be preferred over a friend function if it’s possible to do so with the existing member functions available (the less functions touching your classes’s internals, the better). However, don’t add additional access functions just to overload an operator as a normal function instead of a friend function!

Rule: Prefer overloading operators as normal functions instead of friends if it’s possible to do so without adding additional functions.

9.3 -- Overloading the I/O operators
9.2 -- Overloading the arithmetic operators using friend functions

17 comments to 9.2a — Overloading operators using normal functions

  • omri

    According to my questions and replies from 8.5, I think the following rephrasing is due:
    return Cents(c1.m_cents + c2.m_cents);
    is not an explicit call to the Cents(int x) constructor, but rather a syntax to create an anonymous Cents object with the required m_cents member. This anonymous object is returned to the caller etc.

    • Alex

      It’s not a direct call to the Cents() constructor, but it does use the Cents() constructor, so saying we’re “using” the constructor is appropriate.

  • Omri

    // note: this function is not a member function!
    Cents operator+(const Cents &c1, const Cents &c2)
    {// use the Cents constructor and operator+(int, int)
    // we can access m_cents directly because this is a friend function
    return Cents(c1.m_cents + c2.m_cents);
    Is the following correct?
    // note: this function definition is not a member function definition!
    Cents operator+(const Cents &c1, const Cents &c2)
    {/*lt uses the Cents constructor explicitly and can access objects c1, c2 m_cents member directly because this is a Cents friend function*/
    return Cents(c1.m_cents + c2.m_cents);

  • Omri

    In the first code section above:
    // add Cents + Cents using a friend function
    friend Cents operator+(const Cents &c1, const Cents &c2)
    would it be correct to say here:
    /*add Cents + Cents using a friend function named operator+
    note that this function is not a member function of the class.
    It is an "ordinary" function but special in the fact that it is given permission "by the class" to access this class’s private members*/
    friend Cents operator+(const Cents &c1, const Cents &c2)

  • John

    Thank you for including an example using separate .h and .cpp files! Seeing the & in front of the parameters in the declaration

    helped solve a problem for me! I was only including the & in my function definition in my .cpp file.

  • Connor

    Hi Alex, why are we including the function prototype for

    in the header file?
    Since the .cpp file must be included in the solution is there anything wrong with defining the

    only in the .cpp file?

    • Alex

      We put the forward declaration of operator+ in the header so that any file that #includes Cents.h can use that operator+ without additional work (other than compiling Cents.cpp into the project).

      If you put the forward declaration in Cents.cpp, main.cpp won’t see it there, since main.cpp can’t see what’s other code files while its being compiled.
      If you put the forward declaration in main.cpp, it would work for this program. But in a larger program, you’d have to do the same for every file that wanted to use this function, and that’s a pain.

  • Devaraj

    Hi Alex,
    In the first code above , overloading function is not using the access function getcents(), than how it can access private members ? Hope the code is wrong!

    • Alex

      If a class makes a function a friend, that function can access the private members of the class as if it were a member of the class itself. In this case, our overloaded operator+ is a friend of class Cents, so it can access private member m_cents directly.

      That’s the whole point of the lesson! 🙂


    Hi Alex… really nice lectures… thanks a ton…

    int getCents() const { return m_cents; }…

    why are we using the _const_ keyword above? Can you also please link where was this part discussed previously in chapters?


  • Darren

    "…touching your classes’ internals,…" there’s a joke in there somewhere.

  • Shiva

    This is the first time I’m seeing a chapter on this site without even a single comment, so I’ll add one 😉 :

    > Because of this, we can write our overloaded operator+ as a non-member: (in this context non-friend would be more appropriate)

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter