How do I overload the operator * when my object is on the right side in C++?
问题 I want to implement "operator * " overloading INSIDE my class, so I would be able to do the following: Rational a(1, 2), b; b = 0.5 * a; // b = 1/4 Notice that b is on the right side, is there a way to do such a thing inside "Rational" class? 回答1: No. You must define operator* as a free function. Of course, you could implement it in terms of a member function on the second argument. 回答2: Yes: class Rational { // ... friend Rational operator*(float lhs, Rational rhs) { rhs *= lhs; return rhs;