home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!usc!news.cerf.net!nic.cerf.net!hlf
- From: hlf@nic.cerf.net (Howard Ferguson)
- Newsgroups: comp.lang.c++
- Subject: Re: Restatement and clarification of "static inheritance" question
- Date: 8 Jan 1993 17:42:43 GMT
- Organization: CERFnet Dial n' CERF Customer Group
- Lines: 90
- Sender: hlf@cerf.net
- Message-ID: <1ikeekINN1sb@news.cerf.net>
- References: <1993Jan8.154446.17174@dayfac.cdc.com>
- NNTP-Posting-Host: nic.cerf.net
- Keywords: inheritance
-
- In article <1993Jan8.154446.17174@dayfac.cdc.com> pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;) writes:
- >
- >RESTATEMENT OF THE PROBLEM
- >
- >>Consider class A which counts its live instantiations.
- >
- > #include <iostream.h>
- >> class A {
- >> static int nr;
- >> public:
- >> A() {nr++;}
- >> ~A() {nr--;}
- >> static int getNrInstances (){return nr;}
- >> };
- >>* int A::nr = 0;
- >
- >>A has useful functionality which I would like to use in other classes
- >>with MINIMAL change to the other classes. One way is to try inheritance:
- > ---
- >>* class B :public A {};
- >>* class C: public A {};
- >
- >> int main (int, char**) {
- >> B b1,b2,b3;
- >> C c1,c2;
- >>* cout << B::getNrInstances () << " instances of B ? \n";
- >>* cout << C::getNrInstances () << " instances of C ? \n";
- >> return 0;
- >> }
- > ----- output using Borland C++ v2.0-----
- > 5 instances of B ?
- > 5 instances of C ?
- >
- >This seems to show that inheritance ties the FUNCTIONALITY to
- >the A subpart of B.
- >
- >WHAT I AM INTERESTED IN IS THE FUNCTIONALITY OF "COUNTING ONE'S
- >OWN INSTATIATIONS", NOT COUNTING THE TOTALITY OF ALL INSTATIATIONS OF
- >CLASS A. THEREFORE THE OUTPUT I AM LOOKING FOR IS
- >
- > ---desired output------
- > 3 instances of B ?
- > 2 instances of C ?
- >
- >Could anyone enlighten me as to how to acquire the functionality of a
- >class like A that (that depends on a static variable) with LEAST
- >modification to the acquiring class ? It doesn't have to be with
- >inheritance. I would like to include this functionality for debugging
- >The test program would call the class B as in B::getNrInstances().
- >I would prefer not calling a B instance b1.getNrInstances(). The number
- >returned should be dependent only on the class and not on the particular
- >instance called (that's why I said static inheritance.) Am I being more
- >clear ? Thanks.
-
- At first grance I would say
- 1. If you are going to inherit make A an abstract data type by
- making its destructor a pure virtual function (see Meyers Effective
- C+, item 14)
-
- 2. You probably do not want to inherit anyway because inheriting
- means you want to model IS_A, and this is not the case. Imagine
- if you ended up using a pointer to an A somewhere in your code.
- You would have no way of knowing what kind of object it really is-
- (I can send you a previous discussion on "Inheriting all objects
- from a single parent" if you want).
-
- 3. I do not have a templating compiler ,so I was not able to try
- this but you might be able to try something like this.
-
- Make A a template class, so that a new A class is created for each
- class that it is used in. Then contain A instead of inheriting (see
- 2 points above).
-
- class B {
- private :
- A<B> a;
- }
-
- This is a much smaller change to the class than inheritting.
-
- Now something like the following might work :
-
- inty number = A<B>::GetNrInstances();
-
- This scheme whould also allow you to count the instances of a parent
- or each of its children an you choose, by including an a in which
- ever classes you wish to count. Your inheritence method would have
- run into trouble in that sort of a situation.
-
- hlf
-