Possible Duplicate:
Operator Overloading in C++ as int + obj
I override the * operator as following:
Point Point::operator *(float scale){
Point point(this->x*scale, this->y*scale);
return point;
}
How can I fix this:
Point p1 (5.0, 10.0);
Point p2 = p1*4; //works fine
Point p3 = 4*p1 //error: no match for 'operator*'
Write a free function, like this:
Point operator *(float scale, Point p)
{
return p * scale;
}
You overload the operator as a free function and provide both the versions of it instead of overloading it as member function.
Point operator *(float scale, Point p);
Point operator *(Point p, float scale);
With these:
1st version supports:
Point p3 = 4*p1;
and 2nd version supports:
Point p2 = p1*4;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With