Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!apple!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!unido!orthogo!basti From: basti@orthogo.UUCP (Sebastian Wangnick) Newsgroups: comp.lang.c++ Subject: Virtual functions used in constructor illegal ! ?? Keywords: virtual functions, constructor Message-ID: <822@orthogo.UUCP> Date: 23 Nov 90 08:14:42 GMT Distribution: comp Organization: none Lines: 50 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. Here an example program that you can compile to see whether other compilers do the same: extern "C" void abort (); extern "C" void printf (...); typedef int Rect; class Window { public: Window (Window* parent = 0); virtual Rect Size (); virtual void ChildAdd (Rect size) {} }; Window::Window (Window* parent) {if (parent) parent->ChildAdd(Size()); } Rect Window::Size () {printf("SubclassResponsibility\n"); abort(); return 0;} class NullWindow: public Window { public: NullWindow (Window* parent = 0) : (parent) {} virtual Rect Size () {return 0;} }; void main () {Window* parent = new NullWindow; Window* child = new NullWindow(parent); } What will happen if you compile this? Well, my cfront2.0-generated code returns "SubclassResponsibility" because from within the constructor of class Window the function Window::Size is called, not NullWindow::Size as I expected! Now: Is this a bug or a feature ?? Sebastian Wangnick (basti@orthogo.uucp)