c++ - Operator overloading with non-member function? -
i reading effective c++ , came across this:
class rational { ... }; const rational operator*(const rational& lhs, const rational& rhs); is operator overloading? if yes, why in weird form?
thanks in advance.
is operator overloading? if yes, why in weird form?
yes. form not "weird". 1 form allows multiply 2 rational numbers together.
operators can overloaded member functions or non-member functions. main advantage of using non-member function (like this) you're making clear you're not accessing private state of rational class, nor modifying left hand operand (which clear due const).
the member function versions required if you're going modify left hand operand (this) or need access private variables within types, requires part of class. otherwise, access private state require declared friend function.
Comments
Post a Comment