home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18919 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.9 KB  |  96 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: <1993Jan11.051953.9229@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> <86431@ut-emx.uucp>
  10. Date: Mon, 11 Jan 1993 05:19:53 GMT
  11. Lines: 83
  12.  
  13. In article <86431@ut-emx.uucp> jamshid@emx.utexas.edu writes:
  14. >I quote a few different articles below -- watch the attrib lines.
  15. >
  16. >In article <1993Jan4.144320.5586@dayfac.cdc.com> pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;) writes:
  17. >>Consider class A which counts its 'live' instantiations.
  18. >
  19. >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) answers in article <1993Jan5.051940.28930@ucc.su.OZ.AU>
  20. >|    IF: you want to count all objects allocated (of any type):
  21. >|
  22. >|    Sure: two type of class: base classes and working 
  23. >|classes. The base classes NEVER inherit A.
  24. >|The working classes inherit from A when you want a counter.
  25. >|It is not allowed to derive from a working class though.
  26. >|[code deleted]
  27. >
  28. >Not allowing one to derive from the "working" class is too serious a
  29. >restriction, IMHO.
  30.  
  31.     Not if you follow the paradigm consistently.
  32. The "working" classes will always be derived 'at the last minute'
  33. and preferably anonymously.
  34.  
  35.     For each class A, you have a class workA. If you want
  36. to derive from 'workA' dont-just derive from 'A'. I cant
  37. see any restriction here, just boring typing maintaining
  38. two sets of classes. 
  39. >|
  40. >|    class Counter {
  41. >|        virtual int* counter()=0;
  42. >|    public:
  43. >|        Counter() { (*counter())++; }
  44. >|        ~Counter() { (*counter())--; }
  45. >|        int TellCount()const {return *counter();}
  46. >|    };
  47. >
  48. >Famous last words :-).  I do agree it would be a yucky solution, but
  49. >fortunately it just doesn't work.  The Counter ctor/dtor are calling
  50. >a virtual function (and a pure one at that) -- make sure your
  51. >compiler sternly warns about this.
  52.  
  53.     You're right!
  54. >
  55. >      class A : public KeyCount<A> {/*...*/};
  56. >
  57. >That's it.  Comments?  
  58.  
  59.     It doesnt work :-)
  60.  
  61.     class A : public KeyCount<A> { ... }
  62.     class Z : public A, public Keycount<Z>
  63.     {
  64.         A a;
  65.     };
  66.     Z z;
  67.     A a;
  68.  
  69. There is one 'A' object and one 'Z' object agree?
  70.  
  71. But the counts show:
  72.  
  73.     Z==1; // correct
  74.     A==3; // woops!
  75.  
  76. To get this right one can follow a strict paradigm:
  77.  
  78.     class workA : public A, public KeyCount<A> {};
  79.     workA a;
  80.     class Z : public A { A a; };
  81.     class workZ : public Z, public KeyCount<Z> {};
  82.     workZ z;
  83.  
  84.     // always allocate a 'work' class to construct a whole
  85.     // object
  86.     // never contain or derive from this class
  87.  
  88. Counts are now: A==1, Z==1. Correct.
  89.  
  90.     
  91. -- 
  92. ;----------------------------------------------------------------------
  93.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  94.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  95. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  96.