is it possible to overload power operator in C++

Started by
8 comments, last by iMalc 15 years, 9 months ago
Hi all: Is it possible to overload the power operator ^ in C++? As I see in C++, pow(x, y) is used for x^y. But it would be more convenient if ^ can be overloaded for the same purpose. How to do it please? Or is it possible? Thanks [Edited by - Asuralm on July 15, 2008 9:31:59 AM]
Asura
Advertisement
For your own types, yes it's possible. For primitive types, no - it's used for exclusive or.

Example:
MyClass operator ^ (const MyClass& a, const MyClass& b){   MyClass retVal;   // Fill in retVal in whatever way you want from a and b   return retVal;}
I wouldn't recommend it of course. Operators should retain their natural meaning. You can define * on vectors to do addition, but that'd produce horribly confusing code. Similary, ^ means XOR in C++. To redefine it to "power" on certain types would just be confusing, IMO.

Just because you can doesn't mean you should [smile]
Million-to-one chances occur nine times out of ten!
Quote:Original post by Mike nl
I wouldn't recommend it of course. Operators should retain their natural meaning. You can define * on vectors to do addition, but that'd produce horribly confusing code. Similary, ^ means XOR in C++. To redefine it to "power" on certain types would just be confusing, IMO.

Just because you can doesn't mean you should [smile]
I've seen operator* used for dot product and operator ^ used for cross product in several vector libraries out there. I'm sure it makes sense to the author, but there's no reason not to have a dotProduct() and crossProduct() function...
Quote:Original post by Mike nl
I wouldn't recommend it of course. Operators should retain their natural meaning. You can define * on vectors to do addition, but that'd produce horribly confusing code. Similary, ^ means XOR in C++. To redefine it to "power" on certain types would just be confusing, IMO.

Just because you can doesn't mean you should [smile]


I think ^ is quite natural for exponentiation - it's used in other languages and in mathematics. Besides XOR is rarely used on integers that represent numbers (as opposed to integers that are being treated as just bits), so I think this use is perfectly acceptable.

But as Steve pointed out this can only be done for custom types, not ints or doubles.
Quote:Original post by Simian Man
Quote:Original post by Mike nl
I wouldn't recommend it of course. Operators should retain their natural meaning. You can define * on vectors to do addition, but that'd produce horribly confusing code. Similary, ^ means XOR in C++. To redefine it to "power" on certain types would just be confusing, IMO.

Just because you can doesn't mean you should [smile]


I think ^ is quite natural for exponentiation - it's used in other languages and in mathematics. Besides XOR is rarely used on integers that represent numbers (as opposed to integers that are being treated as just bits), so I think this use is perfectly acceptable.

But as Steve pointed out this can only be done for custom types, not ints or doubles.


Although I agree with you that the usage of ^ for exponentiation is commonplace, I still have to argue that since it already has a different meaning in C++, one which can not be altered, it would, at least in my opinion, be confusing if it were to have a different meaning in a custom type.
Best regards, Omid
Operator precedence? What would you expect the result of a + b ^ c + d to be?
Quote:Original post by Oluseyi
Operator precedence? What would you expect the result of a + b ^ c + d to be?


Good point. Another problem is associativity: XOR is left associative, but exponentiation is right associative. You've convinced me [smile].
Quote:Original post by Asuralm
Hi all:

Is it possible to overload the power operator ^ in C++?

As I see in C++, pow(x, y) is used for x^y. But it would be more convenient if ^ can be overloaded for the same purpose.

How to do it please? Or is it possible?

Thanks


^ is not "the power operator". The language defines what operators mean in the language. Mathematics does not.

It is not possible to change what the operators mean for built-in types. You can make them mean whatever you like for your own types, but people will yell at you. An experienced C++ programmer expects ^ to do something XOR-like to your objects, rather than something exponentiation-like, no matter how strong his mathematical background is.
Such an idea is somewhat less forgiveable than using << for stream output and left-shifting.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement