Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: Static initialization Keywords: static initialize virtual Message-ID: <72347@microsoft.UUCP> Date: 14 May 91 19:41:15 GMT References: <1991May10.063358.15955@ccu1.aukuni.ac.nz> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 34 In article <1991May10.063358.15955@ccu1.aukuni.ac.nz> gkean@ccu1.aukuni.ac.nz (Graham Kean;;g_kean) writes: |Can anyone tell me why it is not possible to statically |initialize an instance of a class containing virtual functions? | |class TESTCLASS | { | public: | int a,b,c; | virtual void testfunc(); | }; |void TESTCLASS::testfunc() {} |TESTCLASS test = {1,2,3}; The problem is not that you are trying to statically initialize an instance of a class containing virtual functions, but rather that you are trying to initialize a class with virtual functions with an initializer list. ARM 8.4.1 states that you are only allowed to use initializer lists with objects of classes with no constructors, no private nor protected members, no bases classes, nor any virtual functions. I don't see where there would be any great difficulty for most compilers to statically initialize your vtable pointer, its just that the standard tells them not to do so. I'd guess the rationale is that if you want to be using inheritence and polymophism, you really "ought" to be be doing it the C++ way, using constructors to get your object to a safe default state, and then use member functions to modify that state. Alternatively, for C backwards compatibility with C-like structures, C++ still supports initializer lists. Compilers could be required to support strategies that fall in between, but I guess it wasn't considered worthwhile. With the present rules, compiler writers are assured that all vtable ptr initializations can happen within compiler generated constructors. Your desired feature would require them to be a little bit smarter.