I'm currently creating a class for complex numbers, and so I figured to make it easier, I'd allow for operations such as a = b + c, instead of a = b.add(c). For example, here is my implementation of addition of two complex numbers:
// An addition operator for the complex numbers
Complex Complex::operator + (Complex n) {
return Complex(this->real + n.real, this->imaginary + n.imaginary);
}
In this example, adding complex numbers a + b would have the same result as adding b + a, as they should.
However, the issue comes when dealing with non-commutative operators and integers. For example, division by an integer or division of an integer. How could I make it such that both:
a = complex / integer
and
a = integer / complex
give correct responses?
In other words, how can I overload operators in two ways?