Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c++ Subject: Re: Static initialization Keywords: static initialize virtual Message-ID: <722@taumet.com> Date: 11 May 91 19:19:36 GMT References: <1991May10.063358.15955@ccu1.aukuni.ac.nz> Organization: Taumetric Corporation, San Diego Lines: 30 gkean@ccu1.aukuni.ac.nz (Graham Kean;;g_kean) writes: >Both Zortech C++ and Borland C++ give an error at the >'{1,2,3}' in the following example: >class TESTCLASS > { > public: > int a,b,c; > virtual void testfunc(); > }; >void TESTCLASS::testfunc() {} >TESTCLASS test = {1,2,3}; The C++ rule is that a class may be statically intialized using C-style initialization syntax as long as it doesn't have "class-like" features. Class-like features require a constructor to be called, which is not supported by the C syntax. You can look up in the ARM to find the full details of what is allowed. The easiest way to remember it is that a class can be initialized with C syntax if it looks like a C struct. Otherwise, use a constructor. (This is overly restrictive, but will keep you out of trouble.) In your specific instance, a class with virtual functions requires a (pointer to the) table of the virtual functions to be part of the class, something done by the constructor. The '={1,2,3}' syntax precludes calling a contructor, and hence is illegal. -- Steve Clamage, TauMetric Corp, steve@taumet.com