I found an example of how to overload arithmetic operators using friend functions, and the overloaded operator function is defined inside the class with comments stating that:
/* This function is not considered a member of the class, even though the definition is
inside the class */
this is the example:
#include <iostream>
class Cents
{
private:
int m_cents {};
public:
Cents(int cents) : m_cents{ cents } { }
// add Cents + Cents using a friend function
// This function is not considered a member of the class, even though the definition is inside the class
friend 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};
}
int getCents() const { return m_cents; }
};
int main()
{
Cents cents1{ 6 };
Cents cents2{ 8 };
Cents centsSum{ cents1 + cents2 };
std::cout << "I have " << centsSum.getCents() << " cents.\n";
return 0;
}
Is this function really not a member of that class? and are all friend functions defined inside the class but not a member of that class or It is only for overloaded functions using the friend keyword.