Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!sics.se!fuug!news.funet.fi!tukki.jyu.fi!sakkinen From: sakkinen@jyu.fi (Markku Sakkinen) Newsgroups: comp.lang.c++ Subject: Re: Inheritance & type conversion Message-ID: <1991Jun13.103513.3889@jyu.fi> Date: 13 Jun 91 10:35:13 GMT References: <2348@taurus.cs.nps.navy.mil> <1991Jun6.140159.9208@rathe.cs.umn.edu> Organization: University of Jyvaskyla, Finland Lines: 74 I have not followed this group regularly in recent times. Pardon me if there has already been a correct answer to the original question, but I have only seen the following incorrect one. In article <1991Jun6.140159.9208@rathe.cs.umn.edu> ian@rathe.cs.umn.edu (Ian Hogg) writes: >In article <2348@taurus.cs.nps.navy.mil> skip@taygeta.oc.nps.navy.mil (Skip Carter) writes: >> ... >> 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. > > You don't need to define a conversion from Derived to Base. An object > of type derived can be used anywhere an object of base is expected. Not quite true. When assigning a _value of_ (not a reference to) Derived to a Base variable, the default operation simply copies the data members of Base and ignores those added in Derived. If that is not desired, a conversion function has to be written. -- However, it might happen that people unnecessarily write conversion functions to achieve just what they would get with the default conversion. >> 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. Turbo seems to be wrong, Zortech right. >> BUT: >> >> Derived dtype; >> >> report2( dtype ); >> >> >> cases a conversion to Base with Zortech C++, BUT NOT for Turbo C++ Again Turbo does it wrong and Zortech right! Score 2 - 0 for Zortech. An aggravating circumstance is that the pertinent rules in the language definition have been the same already before Release 2.0. It is thus not a question about a novelty, which implementors could be uncertain about or might not have been able to take into account yet. Some years ago, Turbo Pascal seemed to be more Turbo than Pascal in several aspects. Is that the current case with Turbo C++? >> ... > > I don't know which is correct but I would expect no conversion to take place > in either place. The compiler should only apply conversions whenever > necessary, and they aren't in this case. Your doubt about your answer was well founded :-) ---------------------------------------------------------------------- "All similarities with real persons and events are purely accidental." official disclaimer of news agency New China Markku Sakkinen (sakkinen@jytko.jyu.fi) SAKKINEN@FINJYU.bitnet (alternative network address) Department of Computer Science and Information Systems University of Jyvaskyla (a's with umlauts) PL 35 SF-40351 Jyvaskyla (umlauts again) Finland ----------------------------------------------------------------------