home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18839 < prev    next >
Encoding:
Text File  |  1993-01-08  |  3.3 KB  |  90 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gatech!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!udcps3!dayfac!pault
  3. From: pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;)
  4. Subject: Restatement and clarification of "static inheritance" question
  5. Message-ID: <1993Jan8.154446.17174@dayfac.cdc.com>
  6. Summary: looking for help to inherit functionality
  7. Keywords: inheritance
  8. Organization: Control Data Corporation, Dayton, Ohio
  9. Date: Fri, 8 Jan 93 15:44:46 GMT
  10. Lines: 78
  11.  
  12. First off, thanks to all those who responded to my question and my
  13. apologies for not being clear. I probably should have chosen a different
  14. phrasing of the problem and certainly should have explained it better!
  15. First to answer the replies received so far and I will clarify.
  16.  
  17. rweaver@otc.otca.oz.au
  18. >How does this variation, on your theme, strike you?
  19. >It seems to me, to work as you wished.  
  20. ANSWER:see "desired output" below
  21.  
  22. uunet.UU.NET!uunet!segfault!rfg (Ron Guilmette)
  23. >Could you please tell me which compiler you are using, 
  24. > and why you think it does not cause static data members 
  25. > to be inherited?
  26. ANSWER 1.Borland C++ v2.0
  27.        2.Sorry I wasn't clear. I want to inherit the FUNCTIONALITY
  28.          of A, not (necesarily) its data member. See desired output
  29.          below.
  30.  
  31. bradav%udcps3@bradav.Auto-trol.COM (Brad Davidson)
  32. >My question would be does class B and Class C initailize class A?
  33. >In that class B : public A {B() : A() {} }
  34. >will get the desired effect.
  35. ANSWER Sorry, I don't understand. Could you elaborate ?
  36.  
  37.  
  38.           
  39. RESTATEMENT OF THE PROBLEM
  40.  
  41. >Consider class A which counts its live instantiations.
  42.  
  43.      #include <iostream.h>
  44. >    class A {
  45. >        static int nr;
  46. >      public:
  47. >        A() {nr++;}
  48. >       ~A() {nr--;}
  49. >        static int getNrInstances (){return nr;}
  50. >    };
  51. >*      int A::nr = 0;
  52.  
  53. >A has useful functionality which I would like to use in other classes
  54. >with MINIMAL change to the other classes. One way is to try inheritance:
  55.                                            ---
  56. >*   class B :public A {};
  57. >*   class C: public A {};
  58.  
  59. >    int main (int, char**) {
  60. >            B b1,b2,b3;
  61. >            C c1,c2;
  62. >*           cout << B::getNrInstances () << " instances of B ? \n";
  63. >*           cout << C::getNrInstances () << " instances of C ? \n";
  64. >            return 0;
  65. >    }
  66.     ----- output using Borland C++ v2.0-----
  67.     5 instances of B ?
  68.     5 instances of C ?
  69.  
  70. This seems to show that inheritance ties the FUNCTIONALITY to
  71. the A subpart of B.
  72.  
  73. WHAT I AM INTERESTED IN IS THE FUNCTIONALITY OF "COUNTING ONE'S
  74. OWN INSTATIATIONS", NOT COUNTING THE TOTALITY OF ALL INSTATIATIONS OF
  75. CLASS A. THEREFORE THE OUTPUT I AM LOOKING FOR  IS
  76.  
  77.     ---desired output------
  78.     3 instances of B ?
  79.     2 instances of C ?
  80.  
  81. Could anyone enlighten me as to how to acquire the functionality of a
  82. class like A that (that depends on a static variable) with LEAST
  83. modification to the acquiring class ? It doesn't have to be with
  84. inheritance. I would like to include this functionality for debugging
  85. The test program would call the class B as in B::getNrInstances().
  86. I would prefer not calling a B instance b1.getNrInstances(). The number
  87. returned should be dependent only on the class and not on the particular
  88. instance called (that's why I said static inheritance.) Am I being more
  89. clear ? Thanks.
  90.