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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: static "inheritance" question
  5. Message-ID: <1993Jan5.051940.28930@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1993Jan4.144320.5586@dayfac.cdc.com>
  10. Date: Tue, 5 Jan 1993 05:19:40 GMT
  11. Lines: 106
  12.  
  13. In article <1993Jan4.144320.5586@dayfac.cdc.com> pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;) writes:
  14. >Consider class A which counts its 'live' instantiations.
  15. >            class A { 
  16. >                static int nr;
  17. >              public:
  18. >                A() {nr++;}
  19. >               ~A() {nr--;}
  20. >                int getNrInstances () {return nr;}
  21. >            };
  22. >            ...
  23. >            A::nr = 0;
  24. >A has useful functionality which I would like to use in other
  25. >classes with MINIMAL change to the other classes. One way
  26. >is to try:
  27. >    class B :public A { ....};
  28. >    class C :public A { ....};
  29. >     ...
  30. >    B b1, b2, b3;
  31. >    C c1, c2;
  32. >    cout << B::getNrInstances();
  33. >But this doesnUt work because static variables aren't inherited
  34. >(at least not in my compiler).
  35.  
  36.     What do you mean 'aren't inherited'?
  37.  
  38. >Could anyone enlighten me as to how to acquire the functionality
  39. >of a class like A that depends on a static variable with LEAST
  40. >modification to the acquiring class ? Thanks.
  41.  
  42.     IF: you want to count all objects allocated (of any type):
  43.  
  44.     Sure: two type of class: base classes and working 
  45. classes. The base classes NEVER inherit A.
  46. The working classes inherit from A when you want a counter.
  47. It is not allowed to derive from a working class though.
  48.  
  49.     class B { }
  50.     class C { }
  51.     class D { }
  52.  
  53.     class BCD : public virtual B, public virtual B, public virtual C
  54.     { ... }
  55.  
  56. // BCD is the class you want to play with.
  57.  
  58.     .....
  59.     BCD* bcd;
  60.     {
  61.         class dummy : public virtual BCD, public virtual A
  62.         { dummy() : BCD(...) {} };
  63.         bcd=new dummy;
  64.     }
  65.  
  66. // Dummy is a local class
  67.  
  68. IF: you want to partition the counts, you can do this:
  69.  
  70.     class C1 {}; class C1 {}; class C3 {};
  71.  
  72. and now make 'A' a template class and use A<C1>, A<C2> or A<C3>
  73. as appropriate.
  74.  
  75. IF: you want to count instances of EACH class, you must
  76. declare the static variable for each class .. no getting
  77. around this. However, you CAN automate the increment
  78. and decrement somewhat:
  79.  
  80.     class Counter {
  81.         virtual int* counter()=0;
  82.     public:
  83.         Counter() { (*counter())++; }
  84.         ~Counter() { (*counter())--; }
  85.         int TellCount()const {return *counter();}
  86.     };
  87.  
  88.     class X : 
  89.         public virtual Counter { // (1)
  90.         static int count; // (2) must declare
  91.         int * counter() {return &count;} // (3)
  92.     ...
  93.     };
  94.  
  95. and lines 1-3 could be made a macro:
  96.  
  97. #define autocount public virtual Counter { static ....
  98.  
  99. so
  100.  
  101.     class X : autocount
  102.     ...
  103.     };
  104.  
  105. (Yuk)
  106. (PS: I havent tested this)
  107. Note the use of a private virtual function here :-)
  108.  
  109. -- 
  110. ;----------------------------------------------------------------------
  111.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  112.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  113. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  114.