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

11 comments to 9.2a — Overloading operators using normal functions

  • 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)

  • Darren

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


    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?


  • 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! 🙂

  • 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.

  • 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.

Leave a Comment

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