Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!ncar!gatech!mcnc!borg!rhodopsin!coggins From: coggins@rhodopsin.cs.unc.edu (Dr. James Coggins) Newsgroups: comp.lang.c++ Subject: Auto type conversion selects wrong constructor Message-ID: <4255@borg.cs.unc.edu> Date: 7 Jun 91 11:54:42 GMT Sender: news@cs.unc.edu Lines: 46 I have some classes that need to work together closely so I created constructors for each of them that take an object of each of the other classes. We started seeing some very strange behavior during test runs that we could not explain from our code. It turned out that C++ was doing automatic type conversions using a different path through our class relationships than I wanted. Simplified particulars are as follows: Class Base class Derived1: public Base class Derived2: public Base { { { Base(Base&) Derived1(Derived1&) Derived2(Derived2&) Base(Other&) Derived1(Other&) Derived2(Other&) } Derived1(Derived2&) Derived2(Derived1&) } } Class Other { Other(Other&) Other(Derived1&) Other(Derived2&) } Now when I implemented constructors Derived1::Derived1(Derived2&) and Derived2::Derived2(Derived1&) I referred most of the initialization processing to the base class as in Derived1::Derived1(Derived2& d) : (d) figuring that since d was in fact a kind of Base object that the Base::Base(Base&) constructor would be selected. No such luck. My object d was converted to class Other so that it matched the Base::Base(Other&) constructor. Since Other had different properties from the Derived classes (that's why Other is a different class!) this did not work as expected, leading to some interesting hours of debugging. Ouch! --------------------------------------------------------------------- Dr. James M. Coggins coggins@cs.unc.edu Computer Science Department Questions: "How 'bout them HEELS?" UNC-Chapel Hill Correct response: Chapel Hill, NC 27599-3175 "How 'BOUT them Heels?" and NASA Center of Excellence in Space Data and Information Science ---------------------------------------------------------------------