Possible Duplicate:
Operator Overloading in C++ as int + obj
Operator overloading c++-faq
I have a Point
object with this operator:
Point operator +(float other) const {
return Point(x + other, y + other, z + other);
}
I can perform addition like so:
point + 10
But I can't perform it in reverse order:
10 + point
Is there another operator I need to overload in order to provide this functionality?
Outside of the class:
You normally want to overload the global operator:
Instead of two overloads, you may want to use only one overload:
and then have a ctor to create a Point from a float:
In this case, adding a float to a Point will use the ctor to convert the float to a Point, then use your overloaded operator+ to add those two Points together.
As a free function:
Given an existing
Point& Point::operator+=(float other)
, add these two free functions: