home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18038 < prev    next >
Encoding:
Text File  |  1992-12-15  |  4.1 KB  |  150 lines

  1. Path: sparky!uunet!olivea!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: static members in derived classes
  5. Message-ID: <732@ulogic.UUCP>
  6. Date: 15 Dec 92 21:47:19 GMT
  7. References: <1g7c8fINNdhf@horus.ap.mchp.sni.de>
  8. Organization: negligable
  9. Lines: 139
  10.  
  11. In article <1g7c8fINNdhf@horus.ap.mchp.sni.de> werner@diamant.uucp writes:
  12. >We have problems in initializing static class members in derived classes.
  13. >Look at the following example:
  14. >
  15. >#include <iostream.h>
  16. >
  17. >// base class
  18. >
  19. >class B
  20. >{
  21. >public:
  22. >  B() {}
  23. >  static int i;
  24. >};
  25. >int B::i = 27;                 // unique definition, works as expected 
  26. >
  27. >// subclass
  28. >
  29. >
  30. >class S : public B
  31. >{
  32. >public:
  33. >  S() {}
  34. >  getX( void ) { return i; }
  35. >};
  36. >//int S::i = 55 ;     
  37. >// error (AT&T): S::i not a member of S   
  38. >// error (GNU g++): multiple initializations of static member B::i
  39. >
  40. >void main( void )
  41. >{
  42. > S s;
  43. > cout << s.getX() << "\n";    
  44. > // One gets the value of the base class!! 
  45. >}
  46. >
  47. >Stroustrop (p. 165) writes that static class members act similar to
  48. >global variables. In the above example we would expect that for the
  49. >subclass a new instace of the static variable is created which can be
  50. >initialized in analogy to the base class.
  51. >
  52. >But that seems to be not right. There exists only !!one!! instance of
  53. >the static member which is related to the base class !!and!! the
  54. >subclass.
  55. >
  56. >We don't understand this behavior of C++. Can anyone explain to us.
  57.  
  58. I think the shortest answer is found in ARM, ss9.4 (p179 in my edition,
  59. but then I could not find any reference to "static" on p165 of my ARM,
  60. so I include the subsection number for your convenience):
  61.  
  62.     A static member is not part of objects of a class.
  63.  
  64. I know that I have found this annoying in my own code development.
  65. I wanted a static socket member function that I could for an intertask
  66. message base class to be accessed by task-specific subclases such that
  67. all instances of, say, XXXMgrClass would be using the same socket
  68. to the XXXMgr task.  All instances of YYYMgrClass would be using a
  69. different socket from the one used in XXXMgrClass.
  70.  
  71. I found that I had to declare a new static for each derived class and
  72. then provide a virtual getIdent() member function that returned the
  73. value stored in the static for that subclass even if operating off
  74. a pointer to the base class.
  75.  
  76. This worked something like the program appended to this message.  Note
  77. that if you do note redefine the virtual getIdent() function for class
  78. B you will end up with the value '1' instead of '2' coming out.  Try
  79. commenting the lines marked to see this.
  80.  
  81. Actually I have no static member in the base class in my actual
  82. final message class.  The getSocket() function is a true virtual
  83. being declared as "virtual Socket &getSocket() = 0;", leaving all
  84. implementation up to the base classes.
  85.  
  86. Admittedly it would be VERY nice if there were a static that
  87. reinstantiated (I can't think of a better term for the effect...)
  88. itself with each derived subclass, but there are also uses for
  89. the current behavior, so the best solution would be to allow
  90. both types of behavior, selected somehow by the syntax, but what
  91. that would look like I couldn't imagine.....
  92.  
  93. For now we just have to live with it.        :-(
  94.  
  95.  
  96. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  97. Blasting, bursting, billowing forth with    |
  98. the power of ten billion butterfly sneezes,    |    -Richard Hartman    
  99. Man, with his flaming fire,            |    hartman@uLogic.COM
  100. has conquered the wayword breezes.        |
  101.  
  102. #include <stdlib.h>
  103. #include <stream.h>
  104.  
  105. class A 
  106. {
  107. protected:
  108.     static int i;
  109. public:
  110.     A() {};
  111.  
  112.     virtual int getIdent();
  113. };
  114.  
  115.  
  116.  
  117. class B : public A
  118. {
  119. protected:
  120.     static int i;
  121. public:
  122.     B() {}
  123.     int getIdent();            // comment out to break function
  124. };
  125.  
  126. class C : public A
  127. {
  128. public:
  129.     C() {}
  130.     int getIdent();
  131. };
  132.  
  133. int A::i = 1;
  134. int A::getIdent() { return i; }
  135. int B::i = 2;
  136. int B::getIdent() { return i; }            // comment out to break function
  137. int C::getIdent() { return i; }
  138.  
  139. main()
  140. {
  141.     A a;
  142.     B b;
  143.     C c;
  144.  
  145.     A *p1 = &a, *p2 = &b, *p3 = &c;
  146.  
  147.     cout << a.getIdent() << b.getIdent() << c.getIdent() << endl;
  148.     cout << p1->getIdent() << p2->getIdent() << p3->getIdent() << endl;
  149. }
  150.