Path: utzoo!attcan!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: How smart is operator resolution? Message-ID: <59579@microsoft.UUCP> Date: 5 Dec 90 18:57:14 GMT References: <1747@umriscc.isc.umr.edu> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 37 Issues of overloading are probably too complicated to do justice here. I refer you to pages 307 to 339 of "The Annotated C++ Reference Manual." Consider instead the approach below -- the standard trick of introducing an intermediate helper function to allow polymorphism on a second parameter. const float pi = 3.14159; class figure { // abstract base class definition protected: float r, h; public: virtual float volume(void) = 0; friend ostream& operator<< (ostream& s, figure& self) { return self.shiftout(s); } virtual ostream& shiftout(ostream&) = 0; }; class sphere : public figure { // derived class definition public: sphere(float radius) { r = radius; } virtual float volume(void) { return 4.0 / 3.0 * pi * r * r * r; } virtual ostream& shiftout(ostream&); }; ostream& sphere::shiftout (ostream &strm) { strm << "VOLUME OF SPHERE:\n"; strm << "radius = " << r << "\n"; strm << "volume = " << volume() << "\n"; return strm; } main() { figure *fig = new sphere(2.0); cout << *fig << "\n"; return 0; }