5

So I am trying to overload the operator* so both ways will work:

Myclass * a;
a * Myclass;

When I declare this function everything goes well:

Polynomial operator*(const double d);

But when I try doing it the other direction like this:

Polynomial operator*(Polynomial &, const double d)

I'm getting an error: "too many parameters for this operator function.

What am I doing wrong?

Thanks!

Gil
  • 93
  • 1
  • 7
  • 3
    Try using [global operator](http://stackoverflow.com/questions/1145022/difference-between-global-operator-and-member-operator). – MikeCAT Dec 05 '15 at 14:22
  • This isn't real code. While it's possible to guess what's going on, a good question will provide enough information so that people won't have to guess. – Pete Becker Dec 05 '15 at 14:44
  • @PeteBecker I obviously had problem with the deceleration, didn't want to confuse people with too much code lines for something this simple. – Gil Dec 05 '15 at 14:48
  • @Gil - all you needed to do was provide the class definition (stripped of irrelevant members) and the declaration of whichever of those functions is actually global. If you don't know what's going wrong, you're really not in a position to make judgments about what might confuse other people. As it is, you've shown two function declarations and left people to guess whether they're supposed to be member functions or free functions. – Pete Becker Dec 05 '15 at 14:51

1 Answers1

10

When you overload a binary operator as a member function, the class instance is always the left-hand operator and the argument to the function is the right hand, there's nothing you can do to change it. Not if you want to continue using member only functions.

But if you use a global non-member function you can easily have whichever order you want. For example

class Polynomial { ... };

// Function that allows poly * 12.34
Polynomial operator*(const Polynomial& lhs, const double rhs)
{
    ...
}

// Function which allows 12.34 * poly
Polynomial operator*(const double lhs, const Polynomial& rhs)
{
    ...
}

And if you don't want to reimplement the same code in both functions, and the operator is commutative (like multiplication and addition should be) then you can implement one of the function by calling the other:

Polynomial operator*(const Polynomial& lhs, const double rhs)
{
    ...
}

Polynomial operator*(const double lhs, const Polynomial& rhs)
{
    return rhs * lhs;  // Calls the operator function above
}

Of course, the operator taking the Polynomial object as left-hand side may of course be implemented as a member function.


On a related note, if you have implemented the operator as a member function, e.g.

class Polynomial
{
    ...

    Polynomial operator*(const double rhs)
    {
        ...
    }
};

The the following code

Polynomial poly1(...);
Polynomial poly2 = poly * 12.34;

is equal to

Polynomial poly1(...);
Polynomial poly2 = poly.operator*(12.34);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Cheers! Wonderful answer, helped a lot! – Gil Dec 05 '15 at 14:46
  • 1
    I'll just add that it's considered good practice to return a const object (in this case a const Polynomial) when overloading operators, so that compilers complain when trying to make an unintended assignment to the result (e.g. if (poly1*poly2 = poly3)) – Francis Moy Oct 09 '20 at 16:54