From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,e51913eb1f3e0fed,start X-Google-Attributes: gid109fba,public X-Google-Thread: 1164f8,e51913eb1f3e0fed,start X-Google-Attributes: gid1164f8,public X-Google-ArrivalTime: 1994-09-28 08:25:47 PST Path: bga.com!news.sprintlink.net!deathstar.cris.com!news1.oakland.edu!newsxfer.itd.umich.edu!europa.eng.gtefsd.com!howland.reston.ans.net!cs.utexas.edu!not-for-mail From: zeisel@indmath.uni-linz.ac.at Newsgroups: comp.lang.c++,comp.sys.dec Subject: Q: DEC C++: Conversion bug? Date: 28 Sep 1994 06:30:24 -0500 Organization: UTexas Mail-to-News Gateway Sender: nobody@cs.utexas.edu Message-ID: <00985254.553050A4.5@indmath.uni-linz.ac.at> NNTP-Posting-Host: news.cs.utexas.edu Xref: bga.com comp.lang.c++:30799 comp.sys.dec:6083 It is explictely said in the ARM that a pointer or a reference of a derived class can be converted to a pointer or a reference of its base class. What about the object itself? In a book (Guido Buzzi--Ferraris, "Scientifc C++") I read that it cannot be converted. I tried to test it with DEC C++ V1.3A-0 on a VAX and on an Alpha, OpenVMS(V6.1), using the following program: ---------------------------- #include class Base { public: #ifdef VIRTUAL virtual #endif /* ifdef VIRTUAL */ void identify() {cout << " Base " << endl;} #ifdef CONSTRUCTOR Base() { cout << "This is Base()" << endl; } Base(const Base& a) { cout << "This is Base(const Base&)" << endl; } #endif /* ifdef CONSTRUCTOR */ }; class Derived: public Base { public: #ifdef VIRTUAL virtual #endif /* ifdef VIRTUAL */ void identify() {cout << " Derived " << endl;} }; void f(Base* a) { cout << "This is f" << endl; a->identify(); } void g(Base& a) { cout << "This is g" << endl; a.identify(); } void h(Base a) { cout << "This is h" << endl; a.identify(); } int main() { Base a; Derived b; f(&b); g(b); h(b); a = b; a = (Base)b; f(&a); g(a); h(a); return 0; } ---------------------------- Compiling it, I get two times the same error message: h(b); a = (Base)b; %CXX-E-PASNOCONCLA, (1) In this statement, "b" has a class type and cannot be converted to a class type that is incompatible. Surprisingly enough, the program compiles well and gives the expected results when I #define VIRTUAL, CONSTRUCTOR, or both, or when I use Borland C++, Version 3.1. What says the ARM? 5.4: Explicit Type Conversion, p.67 Any type conversion not mentioned below and not explicitly defined by the user is an error. Any type that can be converted to another by a standard conversion (Chapter 4) can also be converted by explicit conversion. 4.7: The implicit conversions of a reference to a derived class to references to its base class implies implicit conversion of an object of a derived class to objects of its base classes. Is this a bug in DEC C++? By the way, DEC C++ calls the copy constructor of class Base two times before it enters h(b); Borland C++ calls it only one time. Is this only a question of optimization? Newsfeed unreliable, please answer also via EMail. Helmut Zeisel ------------------------------------------------------------------- Helmut Zeisel zeisel@indmath.uni-linz.ac.at Institut fuer Mathematik Universitaet Linz A-4040 Linz Tel +43-732-2468-9225 AUSTRIA/EUROPE Fax +43-732-246810