Jump to content
  • Advertisement
Sign in to follow this  
Asuralm

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.

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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.

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!