is it possible to overload power operator in C++

This topic is 3804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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]

Share on other sites
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;}

Share on other sites
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]

Share on other sites
Quote:
 Original post by Mike nlI 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...

Share on other sites
Quote:
 Original post by Mike nlI 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.

Share on other sites
Quote:
Original post by Simian Man
Quote:
 Original post by Mike nlI 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.

Share on other sites
Operator precedence? What would you expect the result of a + b ^ c + d to be?

Share on other sites
Quote:
 Original post by OluseyiOperator 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].

Share on other sites
Quote:
 Original post by AsuralmHi 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.

Share on other sites
Such an idea is somewhat less forgiveable than using << for stream output and left-shifting.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633704
• Total Posts
3013457
×