Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: disappointed with Pohl's C++ book Message-ID: <6590240@hplsla.HP.COM> Date: 8 Sep 89 17:37:02 GMT References: <2293@hub.UUCP> Organization: HP Lake Stevens, WA Lines: 50 //> The real restriction, according to Stroustrup, is that a static //>member of a class cannot be of a type which has a constructor. Static //>members are also forbidden initializers. (At first thought this //>seems strange. After all, static members are really just global //>variables to the implementation, and global variables can be of //>a type with a constructor. But think about what happens when a //>class definition is included into several files. Who does the //>initialization?) //> //> John Nagle //------------------------------------------------ // Oops -- I just noticed in AT&T's 2.0 release notes pg 4-12 that static // members of a class can now have constructors, and can be initialized. // There are new syntaxes for doing so. I haven't seen this info elsewhere: #include class withConstructor { static withConstructor classname; const char* stringp; public: withConstructor(const char* p) { stringp = p; } void print() { printf("%s\n", stringp); } void printClassname() { printf("class name: "); classname.print();} }; withConstructor withConstructor::classname("withConstructor"); class withStaticMember { static withConstructor classname; int someMembers; public: withStaticMember() : someMembers(1234) {} void printClassname() { printf("class name is: "); classname.print(); } }; withConstructor withStaticMember::classname = "withStaticMember"; void main() { withStaticMember someObject; someObject.printClassname(); withConstructor anotherObject = "something"; anotherObject.printClassname(); }