Contents Up << >>

Can I create a "**" operator for "to-the-power-of" operations?

Nope.

The names of, precedence of, associativity of, and arity of operators is fixed by the language. There is no "**" operator in C++, so you cannot create one for a class type.

If you're in doubt, consider that "x ** y" is the same as "x * (*y)" (in other words, the compiler assumes "y" is a pointer). Besides, operator overloading is just syntactic sugar for function calls. Although this particular syntactic sugar can be very sweet, it doesn't add anything fundamental. I suggest you overload " pow(base,exponent)" (a double precision version is in <math.h>).

BTW: operator^ can work, except it has the wrong precedence and associativity.

  • Friends