Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: Virtual functions used in constructor illegal ! ?? Keywords: virtual functions, constructor Message-ID: <59509@microsoft.UUCP> Date: 3 Dec 90 19:10:45 GMT References: <822@orthogo.UUCP> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Distribution: comp Organization: Microsoft Corp., Redmond WA Lines: 38 In article <822@orthogo.UUCP> basti@orthogo.UUCP (Sebastian Wangnick) writes: |I just recognized that my cfront 2.0 installs the vtable |of a derived class only *after* the constructor of the |base class is called. | |This implies that you are not allowed to use virtual member |functions within the constructor of an abstract base class. .... |Now: Is this a bug or a feature ?? This is a "feature" -- IE this is how the C++ language is defined. While in the base class constructor your object is only a base object, it does not become a derived object until the derived constructor begins to work on it. Compilers may rely on this language definition to play with the nasty bits such as vtable and vbase pointers during construction. Thus, attempting to defeat this language "feature" will be problematic and non-portable at best. Likewise in destructors, while in the derived class destructor your object is a derived object, but afterwards when the base destructor is called your object has regressed to becoming just a base object again. These "features" allow base class constructors and destructors to be implemented in sensible ways despite what future class implementors might do in derived classes. Even the permission to use derived class virtual functions in the derived class constructor could be viewed as problematic, since your derived class object does not fully become such until the end of the derived class constructor. However, it was assumed that derived class writers could reasonably be held responsible for their own actions in using their own virtual functions within their own constructors. But beware: deamons lurk in wait for those who so use virtual functions! [In my opinion, one is best not to do substantive work inside a constructor in the first place. Constructors are best used to do things to place an object into the simplest mode where that object can meet its class invariants. Do your substative work, such as virtual function calls, after the constructors [standard disclaimer]] Brought to you by Super Global Mega Corp .com