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

  1. Newsgroups: comp.lang.c++
  2. From: nikki@trmphrst.demon.co.uk (Nikki Locke)
  3. Path: sparky!uunet!pipex!demon!trmphrst.demon.co.uk!nikki
  4. Subject: Re: static "inheritance" question
  5. Reply-To: nikki@trmphrst.demon.co.uk
  6. References: <1993Jan4.144320.5586@dayfac.cdc.com>
  7. Distribution: world
  8. X-Mailer: cppnews $Revision: 1.30 $
  9. Organization: Trumphurst Ltd.
  10. Lines: 76
  11. Date: Wed, 6 Jan 1993 12:09:22 +0000
  12. Message-ID: <726347362snx@trmphrst.demon.co.uk>
  13. Sender: usenet@demon.co.uk
  14.  
  15. In article <1993Jan4.144320.5586@dayfac.cdc.com> pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;) writes:
  16. > Consider class A which counts its 'live' instantiations.
  17. >  
  18. >             class A {
  19. >                 static int nr;
  20. >               public:
  21. >                 A() {nr++;}
  22. >                ~A() {nr--;}
  23. >                 int getNrInstances () {return nr;}
  24. >             };
  25. >             ...
  26. >             A::nr = 0;
  27. >  
  28. > A has useful functionality which I would like to use in other
  29. > classes with MINIMAL change to the other classes. One way
  30. > is to try:
  31. >  
  32. >     class B :public A { ....};
  33. >     class C :public A { ....};
  34. >      ...
  35. >     B b1, b2, b3;
  36. >     C c1, c2;
  37. >     cout << B::getNrInstances();
  38. >  
  39. > But this doesnUt work because static variables aren't inherited
  40. > (at least not in my compiler).
  41.  
  42. This DOES work, but perhaps not in the way you intended. It counts how 
  43. many objects of type A are created. I.e. the total of all A's, B's and C's
  44. together.
  45.  
  46. > Could anyone enlighten me as to how to acquire the functionality
  47. > of a class like A that depends on a static variable with LEAST
  48. > modification to the acquiring class ? Thanks.
  49.  
  50. I would suggest that A is turned into a template class. I compiled and 
  51. tested the following under Borland C++ ...
  52.  
  53. #include <iostream.h>
  54.  
  55. template <class X> class Counted {
  56.     static int nr;
  57. public:
  58.     Counted() {nr++;}
  59.     Counted(const Counted ©) { nr++; }        // You NEED this !
  60.     ~Counted() {nr--;}
  61.     static int getNrInstances () {return nr;}    // Note static !
  62.     };
  63.  
  64. class A : public Counted<A> {
  65.     };
  66. int Counted<A>::nr = 0;
  67.  
  68. class B : public Counted<B> {
  69.     };
  70. int Counted<B>::nr = 0;
  71.  
  72. class C : public Counted<C> {
  73.     };
  74. int Counted<C>::nr = 0;
  75.  
  76. main()
  77. {
  78.     A a1, a2, a3;
  79.     B b1, b2;
  80.     C c1;
  81.  
  82.     cout << " A:" << A::getNrInstances();
  83.     cout << " B:" << B::getNrInstances();
  84.     cout << " C:" << C::getNrInstances();
  85.     return 0;
  86. }
  87.  
  88. -- 
  89. Nikki Locke,Trumphurst Ltd.(PC and Unix consultancy) nikki@trmphrst.demon.co.uk
  90. trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.
  91.