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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.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: static "inheritance" question
  5. Message-ID: <1993Jan4.144320.5586@dayfac.cdc.com>
  6. Summary: how to inherit static class members or do the equivalent
  7. Organization: Control Data Corporation, Dayton, Ohio
  8. Date: Mon, 4 Jan 93 14:43:20 GMT
  9. Lines: 32
  10.  
  11. Consider class A which counts its 'live' instantiations.
  12.  
  13.             class A {
  14.                 static int nr;
  15.               public:
  16.                 A() {nr++;}
  17.                ~A() {nr--;}
  18.                 int getNrInstances () {return nr;}
  19.             };
  20.             ...
  21.             A::nr = 0;
  22.  
  23. A has useful functionality which I would like to use in other
  24. classes with MINIMAL change to the other classes. One way
  25. is to try:
  26.  
  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.  
  34. But this doesnUt work because static variables aren't inherited
  35. (at least not in my compiler).
  36. Could anyone enlighten me as to how to acquire the functionality
  37. of a class like A that depends on a static variable with LEAST
  38. modification to the acquiring class ? Thanks.
  39.  
  40. pault@dayfac.cdc.com
  41.  
  42.  
  43.