Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: Chameleon objects (calling virtual functions from constructors) Keywords: virtual functions, constructor functions. Message-ID: <9353@microsoft.UUCP> Date: 7 Dec 89 18:57:34 GMT References: <11266@csli.Stanford.EDU> Reply-To: jimad@microsoft.UUCP (Jim Adcock) Organization: Microsoft Corp., Redmond WA Lines: 41 In article <11266@csli.Stanford.EDU> Neil%teleos.com@ai.sri.com writes: >When a class has a constructor and virtual functions, cfront1.2 generates >code within the constructor function to set up the virtual function pointers. >If a class is derived from such a base class, and one of the virtual >functions is called from the base class constructor, the wrong virtual >function is called, since during the execution of the base class constructor, >the virtual function pointer table is set up as for the base class, >and is not changed to show the derived class virtual functions until >the derived class constructor is entered. > >Do other C++ compilers exhibit the same behaviour? > >Neil/. Neil%teleos.com@ai.sri.com ...decwrl!argosy!teleos!neil One would hope so, since this seems to be the behavior described in section 12.7 page 83 of the C++ Reference Manual (short quote:) "Member functions may be called in constructors and destructors. This implies that virtual functions may be called (directly or indirectly). The function called will be the one defined in the constructor's (or destructor's) own class or its bases, but *not* any function redefining it in a derived class. This ensures that unconstructed objects will not be accessed during construction or destruction. For example: ...." --- Given a class Base, and a class Derived, an object created of class Derived is done so by first invoking a Base constructor on the object, at which time the object is a Base, then by invoking a Derived constructor on the object, at which time it has become a Derived. [Likewise in multiple inheritence, a MIDerived starts off solely as a Base1 during the Base1 construction phase, is solely a Base2 during the Base2 construction phase, and only becomes a MIDerived (and thus also a Base1 and Base2) during the MIDerived construction phase.] This description is also true (in reversed order) during destruction. After the Derived destructor is called, the object has reverted to only being a Base, after the Base desctructor is called it isn't (hardly(*)) anything. (hardly(*)) == fudge factor to account for static class member functions operator new and operator delete.