home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18843 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  3.6 KB

  1. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!usc!news.cerf.net!nic.cerf.net!hlf
  2. From: hlf@nic.cerf.net (Howard Ferguson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Restatement and clarification of "static inheritance" question
  5. Date: 8 Jan 1993 17:42:43 GMT
  6. Organization: CERFnet Dial n' CERF Customer Group
  7. Lines: 90
  8. Sender: hlf@cerf.net
  9. Message-ID: <1ikeekINN1sb@news.cerf.net>
  10. References: <1993Jan8.154446.17174@dayfac.cdc.com>
  11. NNTP-Posting-Host: nic.cerf.net
  12. Keywords: inheritance
  13.  
  14. In article <1993Jan8.154446.17174@dayfac.cdc.com> pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;) writes:
  15. >          
  16. >RESTATEMENT OF THE PROBLEM
  17. >>Consider class A which counts its live instantiations.
  18. >     #include <iostream.h>
  19. >>    class A {
  20. >>        static int nr;
  21. >>      public:
  22. >>        A() {nr++;}
  23. >>       ~A() {nr--;}
  24. >>        static int getNrInstances (){return nr;}
  25. >>    };
  26. >>*      int A::nr = 0;
  27. >>A has useful functionality which I would like to use in other classes
  28. >>with MINIMAL change to the other classes. One way is to try inheritance:
  29. >                                           ---
  30. >>*   class B :public A {};
  31. >>*   class C: public A {};
  32. >>    int main (int, char**) {
  33. >>            B b1,b2,b3;
  34. >>            C c1,c2;
  35. >>*           cout << B::getNrInstances () << " instances of B ? \n";
  36. >>*           cout << C::getNrInstances () << " instances of C ? \n";
  37. >>            return 0;
  38. >>    }
  39. >    ----- output using Borland C++ v2.0-----
  40. >    5 instances of B ?
  41. >    5 instances of C ?
  42. >This seems to show that inheritance ties the FUNCTIONALITY to
  43. >the A subpart of B.
  44. >WHAT I AM INTERESTED IN IS THE FUNCTIONALITY OF "COUNTING ONE'S
  45. >OWN INSTATIATIONS", NOT COUNTING THE TOTALITY OF ALL INSTATIATIONS OF
  46. >CLASS A. THEREFORE THE OUTPUT I AM LOOKING FOR  IS
  47. >    ---desired output------
  48. >    3 instances of B ?
  49. >    2 instances of C ?
  50. >Could anyone enlighten me as to how to acquire the functionality of a
  51. >class like A that (that depends on a static variable) with LEAST
  52. >modification to the acquiring class ? It doesn't have to be with
  53. >inheritance. I would like to include this functionality for debugging
  54. >The test program would call the class B as in B::getNrInstances().
  55. >I would prefer not calling a B instance b1.getNrInstances(). The number
  56. >returned should be dependent only on the class and not on the particular
  57. >instance called (that's why I said static inheritance.) Am I being more
  58. >clear ? Thanks.
  59.  
  60. At first grance I would say 
  61. 1. If you are going to inherit make A an abstract data type by
  62. making its destructor a pure virtual function (see Meyers Effective
  63. C+, item 14)
  64.  
  65. 2.  You probably do not want to inherit anyway because inheriting 
  66. means you want to model IS_A, and this is not the case. Imagine
  67. if you ended up using a pointer to an A somewhere in your code.
  68. You would have no way of knowing what kind of object it really is-
  69. (I can send you a previous discussion on "Inheriting all objects
  70. from a single parent" if you want).
  71.  
  72. 3. I do not have a templating compiler ,so I was not able to try
  73. this  but you might be able to try something like this.
  74.  
  75. Make A a template class, so that a new A class is created for each
  76. class that it is used in. Then contain A instead of inheriting (see
  77. 2 points above).
  78.  
  79.     class B {
  80.         private :
  81.             A<B> a;
  82.     }    
  83.  
  84.  This is a much smaller change to the class than inheritting.
  85.  
  86. Now something like the following might work :
  87.  
  88.     inty number = A<B>::GetNrInstances();
  89.  
  90. This scheme whould also allow you to count the instances of a parent
  91. or each of its children an you choose, by including an a in which 
  92. ever classes you wish to count. Your inheritence method would have 
  93. run into trouble in that sort of a situation.
  94.  
  95.     hlf
  96.