Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!cit-vax!oddhack!jon From: jon@oddhack.UUCP Newsgroups: comp.lang.c++ Subject: Re: static member data initialization query Message-ID: <2755@cit-vax.Caltech.Edu> Date: Wed, 20-May-87 01:02:53 EDT Article-I.D.: cit-vax.2755 Posted: Wed May 20 01:02:53 1987 Date-Received: Thu, 21-May-87 04:40:01 EDT References: <2464@nlm-vax.arpa> Sender: news@cit-vax.Caltech.Edu Reply-To: jon@oddhack.Caltech.EDU (Jon Leech) Organization: Caltech Odd Hack Committee Lines: 66 In article <2464@nlm-vax.arpa> lef@nlm-vax.arpa (Larry Fitzpatrick) writes: >Hello, I am rather new to C++ and have a question about the >initialization of static member data. Given a class that has >some static data associated with it, that is, data shared >by all instances rather than replicated, what is the best way >to initialize that data? The following was my first thought > > class date { > static char *mmm[12]={"jan","feb","mar",...}; > ... > } > >but this generates a compiler message "initialization of static members >unimplemented" (or something like that). I can't seem to see how >to initialize the data with a constructor given that you only >want it to happen once, not once for every instance. Here's a kludgy way of doing what you want. It relies on the implementation of static class members as global variables, which are guaranteed to be initialized to 0. Basically in addition to the data you want, you add a static flag which says that the static data has not yet been initialized. Then you let the constructor do it. Note that 8.5.1 in the book says ``No initializer can be specified for a static member, and it cannot be of a class with a constructor.'' Presumably the latter restriction has been removed at some point, since the following example compiles and runs fine under 1.1: #include class foo { static int initialized; // Flag; if non-zero, data has been initialized static double data[2]; // Data to be initialized on first constructor public: foo(); }; main() { foo a, b; } foo::foo() { if (!initialized) { cout << "foo.data is not initialized\n"; data[0] = 3.1415926; data[1] = 2.7182818; initialized = 1; // Flag so we don't do this more than once } else { cout << "foo.data is already initialized\n"; } } When run, this produces foo.data is not initialized (when constructor for a is called) foo.data is already initialized (when constructor for b is called) The overhead of checking the flag is minimal compared to the call and (sometimes) allocation overhead for class members. For your example, you would have to do something like define the month names in a external array (or static to the constructor), and make the static member of class date a reference or pointer to this data. -- Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon) Caltech Computer Science Graphics Group __@/