Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!kth!draken!tut!santra!hutcs.hut.fi!jaa From: jaa@hutcs.hut.fi (Jari Alasuvanto) Newsgroups: comp.lang.c++ Subject: Re: Can you initialize private static data members? Message-ID: <24121@santra.UUCP> Date: 1 Aug 89 05:03:36 GMT References: <11327@brunix.UUCP> <404@odi.ODI.COM> Sender: news@santra.UUCP Reply-To: jaa@hutcs.hut.fi.UUCP (Jari Alasuvanto) Organization: Helsinki University of Technology, Finland Lines: 42 In article <404@odi.ODI.COM> dlw@odi.com writes: > > Given: > class X { > ... > public: > X(char *); // The only constructor > } > > and > > class Y { > private: > static X x; > public: > ... > } According to Stroutrup`s BOOK, a static member cannot be of a class with a a constructor (page 275). Has this been changed in 2.0 ? My problem with static members (for the reason above I have mostly used pointers and allocated them explicitely) is: what is the right place to do the initialization. If done in main, you need to include all the .h files (needing the initialization). The usual way for me doing this has been: - declare one static variable in the class implementation file to indicate if the class static members have been initialized - Access the static member only using member functions, so if it is needed an instance of the class has to exist - In the constructor of that class, checked the static flag variable and initialize if necessary. Does anybody have a nicer solution ? I think that the problem is basically the fact that in C++ clases are not objects as in pure OO languages. Jari Alasuvanto Lab. of Information Proc. Science, Helsinki Univ. of Techology, Finland Internet: jaa@hutcs.hut.fi Bitnet: jaa%finhutcs.bitnet tel: +358-0-451 3236 fax: +358-0-465 077