home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!gatech!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!udecc.engr.udayton.edu!udcps3!dayfac!pault
- From: pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;)
- Subject: Restatement and clarification of "static inheritance" question
- Message-ID: <1993Jan8.154446.17174@dayfac.cdc.com>
- Summary: looking for help to inherit functionality
- Keywords: inheritance
- Organization: Control Data Corporation, Dayton, Ohio
- Date: Fri, 8 Jan 93 15:44:46 GMT
- Lines: 78
-
- First off, thanks to all those who responded to my question and my
- apologies for not being clear. I probably should have chosen a different
- phrasing of the problem and certainly should have explained it better!
- First to answer the replies received so far and I will clarify.
-
- rweaver@otc.otca.oz.au
- >How does this variation, on your theme, strike you?
- >It seems to me, to work as you wished.
- ANSWER:see "desired output" below
-
- uunet.UU.NET!uunet!segfault!rfg (Ron Guilmette)
- >Could you please tell me which compiler you are using,
- > and why you think it does not cause static data members
- > to be inherited?
- ANSWER 1.Borland C++ v2.0
- 2.Sorry I wasn't clear. I want to inherit the FUNCTIONALITY
- of A, not (necesarily) its data member. See desired output
- below.
-
- bradav%udcps3@bradav.Auto-trol.COM (Brad Davidson)
- >My question would be does class B and Class C initailize class A?
- >In that class B : public A {B() : A() {} }
- >will get the desired effect.
- ANSWER Sorry, I don't understand. Could you elaborate ?
-
-
-
- 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.
-