0

How would I go about overloading the multiplication operator as a member function in a way that I can multiply both ways? For example, I know how to overload the multiplication operator as a member class so that this works:

MyClass a = a * 2;

However, I would also like a member function that allowed for this:

MyClass a = 2 * a;

How would I overload the multiplication operator to allow this?

unsername
  • 1
  • 1

1 Answers1

0

Just overload the operator * like this:

MyClass &operator* (int);

Also, use templates so you can multiply int, float,double or whatever

Hearner
  • 2,711
  • 3
  • 17
  • 34