home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!darwin.sura.net!wupost!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!drutx!abasin!weh
- From: weh@abasin.dr.att.com (William E. Hopkins)
- Newsgroups: comp.lang.c++
- Subject: Re: Q: static members and private constructors
- Message-ID: <18607@drutx.ATT.COM>
- Date: 23 Jul 92 19:42:54 GMT
- References: <YDD5OTF@hp832.informatik.hu-berlin.de>
- Sender: news@drutx.ATT.COM
- Organization: AT&T Bell Laboratories
- Lines: 92
-
- In article <YDD5OTF@hp832.informatik.hu-berlin.de>, loewis@informatik.hu-berlin.de (M.v.Loewis) writes:
- |>
- |> In the following program, it is not possible to create static (class)
- |> instances of a class with only private constructors. I suppose it is
- |> a general compiler error:
- |>
- |> class B;
- |> class A{
- |> friend class B;
- |> A(int);
- |> int i;
- |> };
- |>
- |> A::A(int){}
- |>
- |> class B{
- |> static A m;
- |> public:
- |> int func();
- |> };
- |>
- |> int B::func()
- |> {
- |> return m.i;
- |> };
- |>
- |> A B::m(1);
- |>
- |> The problem is the last statement: I think it should be possible to create
- |> the m member of B somehow, since B is a friend of A, but all compilers I
- |> tried complained about sth:
- |> BC: A::A(int) is not accessible in function _STCON_()
- |> g++: In function _GLOBAL_$I$__1Ai(): ...invalid initializer to constructor
- |> for type A
- |> CC: sorry, not implemented: static member B::m of class A with constructor
-
- The error message from cfront 2.1 is clearer on the subject:
-
- "static.c", line 21: error: global scope cannot access A:A(): private member
- 1 error
-
- The Annotated C++ Reference Manual (ARM) is not explicit on this subject,
- but a sentence in section 9.4 ``Static Members'' that is relevant (pg. 180
- of first printing):
-
- Static members of a global class are initialized exactly like
- global objects and only in file scope.
-
- Thus, the initialization of B::m takes place in file scope, which does not
- have access to the private parts of A.
-
- It is not a bug; it is a feature. If you want it to work, you can join the
- ranks of the well known netters attempting to get changes considered by the
- ANSI C++ committee... Good luck!
-
- From the statement in the ARM, it qualifies static members as those of
- global classes, from which I deduced that maybe nesting classes would work.
- I was wrong (having tried various formulations).
-
- Below is a kludge I fashioned to more or less accomplish what you wanted
- (but probably at the expense of allowing some access you wished to avoid):
-
- class B;
- class A{
- friend class B;
- A(int);
- int i;
- public:
- A(); // added public default constructor
- };
-
- A::A(int){}
-
- class B{
- static A m;
- public:
- static int init_m(); // added public initializer function
- int func();
- };
-
- int B::func()
- {
- return m.i;
- };
-
- A B::m; // allowed because A::A() is public
- static int dummy = B::init_m();
-
- int B::init_m () { m = A(1); return 1;}
-
-
- Bill Hopkins
-