Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uakari.primate.wisc.edu!umriscc!mcs213f.cs.umr.edu!jamesh From: jamesh@cs.umr.edu (James Hartley) Newsgroups: comp.lang.c++ Subject: How smart is operator resolution? Message-ID: <1747@umriscc.isc.umr.edu> Date: 27 Nov 90 20:15:43 GMT Sender: news@umriscc.isc.umr.edu Organization: University of Missouri - Rolla Lines: 47 Originator: jamesh@mcs213f.cs.umr.edu I recently tried taking advantage of virtual functions by creating a generic routine which would take a base class pointer and output the appropriate derived class where the insertor was properly overloaded. The following program attempts to illustrate the idea. Turbo C++ balks at the output statement in main() because the insertor is not defined with respect to the base class. Is this operator resolution beyond the restraints of the language, or is this a bug in the compiler? ----------------------- BEGIN CODE ------------------------------------- #include const float pi = 3.14159; class figure { // abstract base class definition protected: float r, h; public: virtual float volume(void) = 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; } friend ostream &operator<<(ostream&, sphere&); }; ostream &operator<<(ostream &strm, sphere &sph) { strm << "VOLUME OF SPHERE:\n"; strm << "radius = " << sph.r << "\n"; strm << "volume = " << sph.volume() << "\n"; return strm; } main() { figure *fig = new sphere(2.0); cout << *fig << "\n"; return 0; } ------------------------- END CODE ------------------------------------- -- James J. Hartley Internet: jamesh@cs.umr.edu Department of Computer Science Bitnet: jamesh@cs.umr.edu@umrvmb.bitnet University of Missouri - Rolla UUCP: ...!uunet!cs.umr.edu!jamesh