-
December 13th, 2005, 04:06 AM
#1
Senior Member
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, 12:09 PM
#2
Hi
Operator overloading[1] is what you want I think.
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,
since operator overloading requires a class/structure for some
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
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
-
December 13th, 2005, 12:47 PM
#3
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 <iostream>
#include <cmath> // 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 << (b^a); // output the value of pow using ^ within () due to operator presedence
return 0;
}
-
December 13th, 2005, 02:40 PM
#4
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.)
I use google....
http://www.cplusplus.com/doc/tutorial/classes2.html
should give you a nice little read on it
-
December 13th, 2005, 08:07 PM
#5
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.
"When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
"There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
"Mischief my ass, you are an unethical moron." - chsh
Blog of X
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|