Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 703
prgDevelop Avatar asked Jan 30 '11 23:01

prgDevelop


1 Answers

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.

like image 163
Oliver Charlesworth Avatar answered Oct 05 '22 14:10

Oliver Charlesworth