[comp.lang.c++] Auto type conversion selects wrong constructor

coggins@rhodopsin.cs.unc.edu (Dr. James Coggins) (06/07/91)

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
---------------------------------------------------------------------
 

ziane@nuri.inria.fr (ziane mikal @) (06/13/91)

I am having trouble compiling the NIH library. The Makefile of directory errfac does some patching of the ..c files, namely the following:

${CC} ${CFLAGS} -F -c errors.c | sed -e 's/__0__builtin/__builtin/g' >errors..c

    Now, this introduces ambiguities as two parameters become identical due to the sed modification. When I remove the sed, it compiles but seems to return an error code as the make stops. Was the sed really necessary ?

    I compiled the files by hand. But the testing of the NIH class fails. array gives me a segmentation fault. Is it related to the sed business ? Or does it have to do with static constructor order of calling of the linker ?

    I'll be grateful to anyone who could provide some help.