Path: utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!tikal!hplsla!jima From: jima@hplsla.HP.COM (jim adcock ) Newsgroups: comp.lang.c Subject: Re: why just a power operator? Message-ID: <5260002@hplsla.HP.COM> Date: 11 Jan 88 21:05:25 GMT References: <3410@megaron.arizona.edu> Organization: HP Lake Stevens, WA Lines: 74 /* Regards: overloading "->" for "pow" Sorry, I should have known better than to try to sell "software futures." Overloading "->" MIGHT have been "nice" since it has about the precedence and binding required for a good power operator. Unfortunately, as Bjarne points out, there are three problems with this: 1) "->" expects a member on its right hand side -- you don't get to "overload" C++ syntax. 2) to overload an operator, at least one operand has to be a class argument, see page 283 of the Mar86 printing. 3) It's likely that writing "x->y" would NOT be interpreted by a human reader as "x to the y power" Hopefully, if my release of the compiler HAD the "->" overloading capability, I would have TRIED IT before I show my mouth off !!! Given C++ overloading restrictions, the following overload of "^" shows what CAN be done in C++. Unfortunately "^" has poor precedence to be used as a "pow" operator, so you'd have to parenthesize it: mycubic = a[3]*(x^3) + a[2]*(x^2) + a[1]*x + a[0]; which MIGHT be better than writing: mycubic = a[3]*pow(x,3) + a[2]*pow(x,2) + a[1]*x + a[0]; [to use a weak example!] By analogy to using "<<" as the output operator, using "^" to mean "pow" MIGHT be justifiable if you need it enough, and use it enough in your code to make its use obvious from context. Again, IFF the power operator is needed by most users of a language, then it should be in the language, otherwise leave it out. P.S. I HAVE tried the following, which is based on the half-baked idea I had in the back of my head when I shot my mouth off last time!!! */ #include extern double pow(double x, double y); class Double { public: double dval; Double(double val) {dval = val;}; }; inline double operator^(Double& x, int y) { return pow(x.dval, double(y)); } main() { int i; Double x = 2.0; for (i = 0; i <= 10; ++i) printf("%g \n",x^i); }