Re: odd behaviour of overloaded * operator
I've created a class and defined how it should multiply with the other
number classes, but I keep getting errors when using it. Recently I
spotted that the errors came when I used:
double * myClass
as opposed to:
myClass * double
Can someone tell me how I should be overloading my operators to avoid
the above problem?
We don't really know! And how could we? Post some code, buddy!
Sorry, this is how I overloaded it:
Vector operator*(double scalar)
result.x = x * scalar;
result.y = y * scalar;
result.z = z * scalar;
That's illegal, but I suspect this is a member function. Next time,
post complete, compilable code.
C operator*(double d);
void f(C& c)
c * 1.0;
This works because the statement becomes
1.0 * c
doesn't, because 1)
makes no sense and 2)
operator*(double, const C&);
doesn't exist (remember: a binary operator @ as in x@y can be applied
as a member function x.operator@(y) or as a namespace scope function
There are three solutions. First, define two operator* at namespace
scope (outside the class):
C operator*(double d, const C& c); // 1.0 * c
C operator*(const C& c, double d); // c * 1.0
These operators may be friends of C if they need to.
The second and third solutions only work if you can [implictly]
construct a C from a double:
In this case, either define one non member operator* that takes two Cs:
C operator*(const C& c1, const C& c2);
or one member operator* that takes one C:
C operator*(const C& c);
The choice between the three solution is not only a matter of taste,
but also depends on the interpretation of the concept of
"encapsulation" or "information hiding". Have fun.
1.0 * c;
creates a temporary C initialized with 1.0 and both Cs are passed to
operator*. Note that operator*'s parameters must be const references
(or by value, which is "innefficient" however), because that's the only
way you will be able to pass the temporary value (technical: this
temporary is an rvalue and cannot be bound to a non-const reference).