# Making C++ operators

• December 13th, 2005, 03:06 AM
Arkimedes
Making C++ operators
I am thinking about making some operators in C++, but I have no clue how to approach it. Something for "to the power of". Alternatively, I could simply make a void method; but I would prefer an operator.
• December 13th, 2005, 11:09 AM
sec_ware
Hi

The idea is to redefine an operator in the following
formal way (here: "to the power of")

Code:

```double operator^(double a,  double b){         return(exp(b*log(a))); }```

However, the above example won't work if I recall correctly,
reason. I do not know whether this is true in general, or whether
it can be bypassed.

One working, but inconsequent example would be

Code:

```struct mydouble{         double value; }; double operator^(mydouble a,  double b){         return(exp(b*log(a.value))); }```

Cheers

P.s. Certainly, the above construction for the power of
is superior to a naive for-loop for two reasons:
(i) it is more general (double in the exponent is possible),
thus the square can be taken
(ii) for an integer "b" larger than 20 or so, it is
faster. The reason: while the for-loop approach grows linearly
in "b" the other does not.

[1] http://msdn.microsoft.com/library/de...tm/overl_9.asp
• December 13th, 2005, 11:47 AM
csl
sec_ware has hit the nail on the head, to overload an operator, atleast one argument must be a user defined type as below

Code:

```#include &lt;iostream&gt; #include &lt;cmath&gt; // include for pow() using namespace std; struct PowerFloat { // define PowerFloat type       float f;       float operator^(float b) { // change the meaning of the ^ operator             return pow(f,b); // compute the actual pow() and return the value       } }; int main() {       float a = 2.0;       PowerFloat b;       b.f = 5;       cout &lt;&lt; (b^a); // output the value of pow using ^ within () due to operator presedence       return 0; }```
• December 13th, 2005, 01:40 PM
whizkid2300
Just to jump in and add my two cents... They are both correct, when overloading an operator you have to do it within a class... If I am not mistaken it only works within that class also.

I wanna say there are some operators that are forbidden also.

But, I do what all great men do when they don't know.... Turn to a great women and tell her to get my coffee... (Please women don't take offense in that... it was a joke.)

http://www.cplusplus.com/doc/tutorial/classes2.html

should give you a nice little read on it
• December 13th, 2005, 07:07 PM
Juridian
Erm, you don't necessarily have to overload an operator in a class. You can make non member operators to do things like the insertion and extraction operators to handle your user defined classes.

As an example:

std::istream& operator>>(std::istream& is, <user defined class>& v )
{
// code input data for your user defined class
return is;
}

This operator can then be used in the standard way to use "cin >> <your class>;" to get data for your class.

You might also look into using templates to make the non member operator more generic.