jps@cs.brown.edu (John Shewchuk) (07/27/89)
Given: class X { ... public: X(char *); // The only constructor } and class Y { private: static X x; public: ... } Is there any way to initialize Y::x? The references seem to indicate that Y::x can not be initialized by saying X Y::x("Test"); because Y::x is private. Is this correct? Thanks. John Shewchuk jps@cs.brown.edu
dlw@odi.com (Dan Weinreb) (07/31/89)
In article <11327@brunix.UUCP> jps@cs.brown.edu (John Shewchuk) writes: From: jps@cs.brown.edu (John Shewchuk) Date: 26 Jul 89 20:30:41 GMT Organization: Brown Computer Science Given: class X { ... public: X(char *); // The only constructor } and class Y { private: static X x; public: ... } Is there any way to initialize Y::x? The references seem to indicate that Y::x can not be initialized by saying X Y::x("Test"); because Y::x is private. Is this correct? No; there's an exception to the access rules so that it can be initialized. According to the Product Reference Manual, page 60, sec 9.4: "Static members obey the usual class member access rules, except that they can be initialized (in file scope)." Experimentation on a simple test case shows that this works properly in Cfront 2.0. Thanks to our local C++ language expert, Sam Haradvala, for researching this and checking it out. Dan Weinreb Object Design, Inc. dlw@odi.com
jaa@hutcs.hut.fi (Jari Alasuvanto) (08/01/89)
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
dlw@odi.com (Dan Weinreb) (08/01/89)
In article <24121@santra.UUCP> jaa@hutcs.hut.fi (Jari Alasuvanto) writes:
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 ?
Yes.
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.
In C++ 2.0, it works just as "extern" variables do in C (and C++).
The initialization appears at file scope (at top level). Typically
the class definition is in a header file, and the initialization is in
one of the source files that includes that header file.
I think that the problem is basically the fact
that in C++ clases are not objects as in pure OO languages.
No, there's no "purity of OO" problem here. The only reason that
there's anything complex going on here is because of the C model of
separate compilation and linking. If everything were always all in
one big file, all of this would be completely simple.
Dan Weinreb Object Design, Inc. dlw@odi.com
Dan.Weinreb@mamab.FIDONET.ORG (Dan Weinreb) (08/05/89)
-- Fidonet: Dan Weinreb via 1:363/9 Internet: Dan.Weinreb@mamab.FIDONET.ORG Usenet: ...!peora!rtmvax!libcmp!mamab!Dan.Weinreb