Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!usc!jarthur!spectre.ccsf.caltech.edu!spectre.ccsf.caltech.edu!myers From: myers@over.cs.caltech.edu (Bob Myers) Newsgroups: comp.lang.c++ Subject: Operator overloading: good style? Message-ID: Date: 19 Feb 90 07:13:52 GMT Sender: news@spectre.ccsf.caltech.edu Organization: California Institute of Technology Lines: 50 In-Reply-To: pcg@aber-cs.UUCP's message of 18 Feb 90 19:12:33 GMT I'd like to hear opinions on operator overloading from a stylistic viewpoint. When is it a good idea? There are some cases that seem very clear to me, but others I have used seem a little questionable to me now. Example: I had a vector3d class I implemented when I first learned C++ about 3 years ago: ---------- class vector3d { double x, y, z; public: vector3d() { x=0; y=0; z=0; } vector3d(double x0, double y0, double z0) { x=x0; y=y0; z=z0; } vector3d operator-(); // Vector negation vector3d operator+(const vector3d&); // Vector addition vector3d operator-(const vector3d&); // Vector subtraction vector3d operator*(double); // Scalar multiplication friend vector3d operator*(double, const vector3d&); // Scalar multiplication vector3d operator/(double); // Scalar division vector3d operator%(const vector3d&); // Cross product vector3d operator^(double); // x-axis rotation vector3d operator&(double); // y-axis rotation vector3d operator|(double); // z-axis rotation // ... etc ... } ---------- You get the idea. Anyway, some of this stuff seemed like obvious candidates for overloading: + - * / for example. However, the overloaded % ^ & | are not at all related to the "standard" C operators. They seemed to be useful operators at the time, however, most particularly the % for cross product. But what about the rotation operators? I found them useful, but will anyone be able to read my code later? It is, I'll admit, difficult to remember which operator rotates the vector about which axis. Is this sort of thing a bad candidate for overloading? What do you think? --------------------------------------------------------------------------- Bob Myers myers@through.cs.caltech.edu