Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!lll-winken!taurus!taygeta.oc.nps.navy.mil!skip From: skip@taygeta.oc.nps.navy.mil (Skip Carter) Newsgroups: comp.lang.c++ Subject: Inheritance & type conversion Message-ID: <2348@taurus.cs.nps.navy.mil> Date: 5 Jun 91 22:34:56 GMT Sender: news@taurus.cs.nps.navy.mil Reply-To: skip@taygeta.oc.nps.navy.mil (Skip Carter) Lines: 141 I have been programming C++ for the past few months, using Turbo C++ V1.0 and Zortech C++ V2.1. In the process of writing an application I came across the following behaviour, which I don't understand: There are two classes, Base and Derived. Derived is derived from the Base class and has a method to do a type conversion from Derived TO Base. Base has friends functions, report1 and report2. Report1 takes a reference to a Base, report2 takes the value of a Base, as the parameter. The following: Derived dtype; report1( dtype ); causes a conversion to Base then report1 is invoked with Turbo C++, but for Zortech C++ NO CONVERSION occurs. BUT: Derived dtype; report2( dtype ); cases a conversion to Base with Zortech C++, BUT NOT for Turbo C++ I am too much of a novice to be able to figure out which is the correct behavior. Can anyone give me some insight ? I have attached a copy of the simple program. Here is the program output for Turbo C++ V1.0: Type conversion from Derived to Base invoked report1 reporting report2 reporting Here is the program output for Zortech C++ V2.1: report1 reporting Type conversion from Derived to Base invoked report2 reporting (Note I have abstracted the classes from the relatively complicated classes in the actual application in order to show the behaviour). The code: // inhtest.cpp tests the interaction of type conversion // and inheritance // Note: // report1( Derived ) causes a conversion to Base // then a call report1( Base ) // with TURBO C++ V1.0, with Zortech C++ V2.1 // no conversion is made // report2( Derived ) causes the OPPOSITE to happen // #ifdef __ZTC__ #include #else #include #endif class Base { public: Base() {}; ~Base() {}; friend void report1(Base& x); friend void report2(Base x); }; void report1(Base& x) { cout << "report1 reporting\n"; } void report2(Base x) { cout << "report2 reporting\n"; } class Derived : public Base { public: Derived() {}; ~Derived() {}; operator Base(); // cast to Base }; // a type cast from a Derived directly to a Base Derived::operator Base() // Base m = Derived a { Base dummy; cout << "Type conversion from Derived to Base invoked\n"; return dummy; } void main() { Derived dtype; report1( dtype ); report2( dtype ); } Everett (Skip) Carter Phone: 408-646-3318 Naval Postgraduate School INTERNET: skip@taygeta.oc.nps.navy.mil Dept. of Oceanography, Code OC/CR UUCP: ...!uunet!taygeta!skip Monterey, CA. 93934 TELEMAIL: s.carter/omnet