home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!zrz.tu-berlin.de!hahn.informatik.hu-berlin.de!loewis
- From: loewis@informatik.hu-berlin.de (M.v.Loewis)
- Subject: Q: static members and private constructors
- Message-ID: <YDD5OTF@hp832.informatik.hu-berlin.de>
- Sender: news@hp832.informatik.hu-berlin.de (H.Boehme U.Kunitz)
- Organization: Humboldt University, Department of Computer Science
- Date: Thu, 23 Jul 1992 12:33:26 GMT
- Lines: 44
-
-
- 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
-
- Obviously, BC and g++ try to create a function which calls the constructors
- for all static instances. This function can't access the private
- constructors. But I didn't please the compilers to do it in such a way,
- and I wasn't able to declare these functions as friends of A.
-
- So, is there a way out of this problem?
-
- --
- Martin
-