home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11404 < prev    next >
Encoding:
Text File  |  1992-07-23  |  1.5 KB  |  55 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!zrz.tu-berlin.de!hahn.informatik.hu-berlin.de!loewis
  3. From: loewis@informatik.hu-berlin.de (M.v.Loewis)
  4. Subject: Q: static members and private constructors
  5. Message-ID: <YDD5OTF@hp832.informatik.hu-berlin.de>
  6. Sender: news@hp832.informatik.hu-berlin.de (H.Boehme U.Kunitz)
  7. Organization: Humboldt University, Department of Computer Science
  8. Date: Thu, 23 Jul 1992 12:33:26 GMT
  9. Lines: 44
  10.  
  11.  
  12. In the following program, it is not possible to create static (class)
  13. instances of a class with only private constructors. I suppose it is
  14. a general compiler error:
  15.  
  16. class B;
  17. class A{
  18. friend class B;
  19. A(int);
  20. int i;
  21. };
  22.  
  23. A::A(int){}
  24.  
  25. class B{
  26. static A m;
  27. public:
  28. int func();
  29. };
  30.  
  31. int B::func()
  32. {
  33.  return m.i;
  34. };
  35.  
  36. A B::m(1);
  37.  
  38. The problem is the last statement: I think it should be possible to create
  39. the m member of B somehow, since B is a friend of A, but all compilers I 
  40. tried complained about sth:
  41. BC: A::A(int) is not accessible in function _STCON_()
  42. g++: In function _GLOBAL_$I$__1Ai(): ...invalid initializer to constructor
  43.     for type A
  44. CC: sorry, not implemented: static member B::m of class A with constructor
  45.  
  46. Obviously, BC and g++ try to create a function which calls the constructors
  47. for all static instances. This function can't access the private
  48. constructors. But I didn't please the compilers to do it in such a way,
  49. and I wasn't able to declare these functions as friends of A.
  50.  
  51. So, is there a way out of this problem?
  52.  
  53. --
  54. Martin
  55.