Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!wuarchive!zaphod.mps.ohio-state.edu!magnus.ircc.ohio-state.edu!csn!arrayb!wicklund From: wicklund@intellistor.com (Tom Wicklund) Newsgroups: comp.lang.c++ Subject: Class members flexible enough? Message-ID: <1991Feb22.190313.4936@intellistor.com> Date: 22 Feb 91 19:03:13 GMT Distribution: na Organization: Intellistor Lines: 61 I've been looking at a few implementations of class "complex" recently and wonder current C++ class members are sufficient. A "simplistic" implementation of complex would look something like: class complex { protected: double re; double im; public: // constructors ... complex operator+(complex c); complex operator-(complex c); . . . }; The operators on complex values (+, -, *, /, etc) are defined as member functions since they operate on the data type "complex". However, in the implementations of complex I've seen I find that member functions are not used, and it appears intentionally avoided whenever possible. The ATT C++ library (as documented in the C++ 2.0 library reference manual, I don't have access to cfront or the actual library) they define all complex operators as friend functions: friend complex operator+(complex c1, complex c2); friend complex operator-(complex c1, complex c2); The GNU g++ library implements complex operators completely outside of the class (they are not even friend functions) via: Complex /* const */ operator + (const Complex& x, const Complex& y); Complex /* const */ operator + (const Complex& x, double y); Complex /* const */ operator + (double x, const Complex& y); Complex /* const */ operator - (const Complex& x, const Complex& y); Complex /* const */ operator - (const Complex& x, double y); Complex /* const */ operator - (double x, const Complex& y); These implementations bring up the questions: 1. Why aren't member functions used? In the ATT case my guess is that the friend implementation allows automatic int/float conversion to complex on either side of the operator. A member function "operator +" won't convert the left hand argument automatically. In the GNU case it looks like somebody decided to implement these functions independent of class representation (the implementation is in terms of public functions and a constructor). 2. If member functions aren't powerful enough to implement a class like complex, is the language lacking something? The whole concept of friend functions seems to point to missing language facilities.