Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!munnari!otc!mikem From: mikem@otc.OZ (Mike Mowbray) Newsgroups: comp.lang.c++ Subject: Re: Static class members Message-ID: <118@otc.OZ> Date: Thu, 7-May-87 02:09:42 EDT Article-I.D.: otc.118 Posted: Thu May 7 02:09:42 1987 Date-Received: Sat, 9-May-87 02:53:52 EDT References: <722@apple.UUCP> Organization: O.T.C. Systems Development, Australia Lines: 57 In article <722@apple.UUCP>, kanner@apple.UUCP (Herbert Kanner) says: > Section 8.5.1 of the C++ Reference Manual (page 275) states: "A > data member of a class may be *static*. ... No initializer can be > specified for a static member, and it cannot be of a class with a > constructor." > > [ ... ] With respect to initializers: I have been unable to find > an instance of a (non-static) class data member with an > initializer; [...] So, what would an explicit initializer do? Nothing, it would be illegal. One initialises non-static data members by passing arguments to their constructors from the constructor of the class. > With respect to a static member of a class that has a constructor: > what could be wrong with that as long as the constructor does not > assign to the static member? As posted sometime ago, the reason is as follows: class definitions appearing in header files will appear in multiple .c files. But static members have to be one and the same for ALL instances of the class. The mechanism by which static instances is initialised is to call a compiler-generated function at the start of main(). However, this function will be inserted in every .o file that used the .h file, and so the initialisation would happen lots of times. There are ways around this, of course, but they are not yet implemented. So there is nothing "wrong with it" as such. Personally, I would like to see it happen, but I understand that other more important things are being worked on. And after all, you can achieve the same effect (almost) by having a static pointer member and using a first-time switch in the constructor, e.g: class SomeClass { .... public: SomeClass(); }; class MyClass { static SomeClass *statmbr; public: MyClass(); }; MyClass::MyClass() { if (statmbr == NULL) statmbr = new SomeClass; // ...etc } Mike Mowbray Systems Development Overseas Telecommunications Commission (Australia) UUCP: {seismo,mcvax}!otc.oz!mikem ACSnet: mikem@otc.oz