Any way to write 'pow' using ^?

I'd like to use the '^' simbol to apply a pow, but its meaning is XOR ...
Any idea ? Can I define a overloaded '^' ?
You can overload it for your own datatypes so it behaves like pow... however the question if that's a good idea I would answer with no. A C++ programmer would be used to ^ meaning XOR, so this might affect readability - especially with stuff like:

1
2
3
4
myInt a(12);
int b = 5;
a^b; //equals a pow 5
b^a; //is this b XOR a or b pow a? 


even if you're used to use ^ as pow, I would advice you against it. But hey, you're the one who'll have to decide in the end, so if you like it better that way I won't stop you.
+1 hanst.

Short answer: You probably shouldn't do it.


EDIT: also note that ^ has the wrong precedence for an exponent operator, so even if you overload it it won't work quite right:

 
x = a*b^c;  // not what you expect... 
Last edited on
Thanks, I understand.
I see my formulas clearly if I could to use ^, but... ok...

And ... could I write some symbol for example ^^, or 'x' , to mean a pow ?

How ?
No, you can only overload already existing operators, not create new ones.

(HOWEVER C++0x allows you to create new suffixes for literals - this doesn't really help here, but it's something you might wanna keep in mind for the future).

EDIT: nin nin
Last edited on
You can't create your own operators. You can only overload existing ones.
In some languages b**x is the exponentiation operator. (But not in C++)
Last edited on
Topic archived. No new replies allowed.