Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!samsung!think!mintaka!mit-eddie!apollo!vinoski From: vinoski@apollo.HP.COM (Stephen Vinoski) Newsgroups: comp.lang.c++ Subject: Re: Chameleon objects (calling virtual functions from constructors) Keywords: virtual functions, constructor functions. Message-ID: <4749b393.20b6d@apollo.HP.COM> Date: 7 Dec 89 18:59:00 GMT References: <11266@csli.Stanford.EDU> Sender: root@apollo.HP.COM Reply-To: vinoski@zep.UUCP (Stephen Vinoski) Organization: Apollo Computer, Chelmsford, MA Lines: 37 In article <11266@csli.Stanford.EDU> Neil%teleos.com@ai.sri.com writes: >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. From Lippman's C++ Primer, page 352: "There are three cases in which an invocation of a virtual function is resolved statically at compile time: 1. When a virtual function is invoked through an object of the class type. . . . 2. When a virtual function is explicitly invoked through a pointer or reference using the class scope operator. . . . 3. When a virtual function is invoked within either the constructor or the destructor of a base class. In both cases, the base class instance of the virtual function is called since the derived class object is either not yet constructed or already destructed." I don't see how it could be done any other way. -steve | Steve Vinoski | Hewlett-Packard Apollo Div. | ARPA: vinoski@apollo.com | | (508)256-6600 x5904 | Chelmsford, MA 01824 | UUCP: ...!apollo!vinoski | | "My second wife isn't even born yet." |