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

  1. 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
  2. From: weh@abasin.dr.att.com (William E. Hopkins)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: static members and private constructors
  5. Message-ID: <18607@drutx.ATT.COM>
  6. Date: 23 Jul 92 19:42:54 GMT
  7. References: <YDD5OTF@hp832.informatik.hu-berlin.de>
  8. Sender: news@drutx.ATT.COM
  9. Organization: AT&T Bell Laboratories
  10. Lines: 92
  11.  
  12. In article <YDD5OTF@hp832.informatik.hu-berlin.de>, loewis@informatik.hu-berlin.de (M.v.Loewis) writes:
  13. |> 
  14. |> In the following program, it is not possible to create static (class)
  15. |> instances of a class with only private constructors. I suppose it is
  16. |> a general compiler error:
  17. |> 
  18. |> class B;
  19. |> class A{
  20. |> friend class B;
  21. |> A(int);
  22. |> int i;
  23. |> };
  24. |> 
  25. |> A::A(int){}
  26. |> 
  27. |> class B{
  28. |> static A m;
  29. |> public:
  30. |> int func();
  31. |> };
  32. |> 
  33. |> int B::func()
  34. |> {
  35. |>  return m.i;
  36. |> };
  37. |> 
  38. |> A B::m(1);
  39. |> 
  40. |> The problem is the last statement: I think it should be possible to create
  41. |> the m member of B somehow, since B is a friend of A, but all compilers I 
  42. |> tried complained about sth:
  43. |> BC: A::A(int) is not accessible in function _STCON_()
  44. |> g++: In function _GLOBAL_$I$__1Ai(): ...invalid initializer to constructor
  45. |>     for type A
  46. |> CC: sorry, not implemented: static member B::m of class A with constructor
  47.  
  48. The error message from cfront 2.1 is clearer on the subject:
  49.  
  50. "static.c", line 21: error: global scope cannot access A:A(): private member
  51. 1 error
  52.  
  53. The Annotated C++ Reference Manual (ARM) is not explicit on this subject,
  54. but a sentence in section 9.4 ``Static Members'' that is relevant (pg. 180
  55. of first printing):
  56.  
  57.     Static members of a global class are initialized exactly like
  58.     global objects and only in file scope.
  59.  
  60. Thus, the initialization of B::m takes place in file scope, which does not
  61. have access to the private parts of A.
  62.  
  63. It is not a bug; it is a feature.  If you want it to work, you can join the
  64. ranks of the well known netters attempting to get changes considered by the
  65. ANSI C++ committee...  Good luck!
  66.  
  67. From the statement in the ARM, it qualifies static members as those of
  68. global classes, from which I deduced that maybe nesting classes would work.
  69. I was wrong (having tried various formulations).
  70.  
  71. Below is a kludge I fashioned to more or less accomplish what you wanted
  72. (but probably at the expense of allowing some access you wished to avoid):
  73.  
  74. class B;
  75. class A{
  76.     friend class B;
  77.     A(int);
  78.     int i;
  79. public:
  80.     A(); // added public default constructor
  81. };
  82.  
  83. A::A(int){}
  84.  
  85. class B{
  86.     static A m;
  87. public:
  88.     static int init_m(); // added public initializer function
  89.     int func();
  90. };
  91.  
  92. int B::func()
  93. {
  94.  return m.i;
  95. };
  96.  
  97. A B::m; // allowed because A::A() is public
  98. static int dummy = B::init_m();
  99.  
  100. int B::init_m () { m = A(1); return 1;}
  101.  
  102.  
  103.         Bill Hopkins
  104.