home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: static "inheritance" question
- Message-ID: <1993Jan5.051940.28930@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <1993Jan4.144320.5586@dayfac.cdc.com>
- Date: Tue, 5 Jan 1993 05:19:40 GMT
- Lines: 106
-
- In article <1993Jan4.144320.5586@dayfac.cdc.com> pault@dayfac.cdc.com (Paul Thompson;DAYFAC-ITS;) writes:
- >Consider class A which counts its 'live' instantiations.
- >
- > class A {
- > static int nr;
- > public:
- > A() {nr++;}
- > ~A() {nr--;}
- > int getNrInstances () {return nr;}
- > };
- > ...
- > 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:
- >
- > class B :public A { ....};
- > class C :public A { ....};
- > ...
- > B b1, b2, b3;
- > C c1, c2;
- > cout << B::getNrInstances();
- >
- >But this doesnUt work because static variables aren't inherited
- >(at least not in my compiler).
-
- What do you mean 'aren't inherited'?
-
- >Could anyone enlighten me as to how to acquire the functionality
- >of a class like A that depends on a static variable with LEAST
- >modification to the acquiring class ? Thanks.
- >
-
- IF: you want to count all objects allocated (of any type):
-
- Sure: two type of class: base classes and working
- classes. The base classes NEVER inherit A.
- The working classes inherit from A when you want a counter.
- It is not allowed to derive from a working class though.
-
- class B { }
- class C { }
- class D { }
-
- class BCD : public virtual B, public virtual B, public virtual C
- { ... }
-
- // BCD is the class you want to play with.
-
- .....
- BCD* bcd;
- {
- class dummy : public virtual BCD, public virtual A
- { dummy() : BCD(...) {} };
- bcd=new dummy;
- }
-
- // Dummy is a local class
-
- IF: you want to partition the counts, you can do this:
-
- class C1 {}; class C1 {}; class C3 {};
-
- and now make 'A' a template class and use A<C1>, A<C2> or A<C3>
- as appropriate.
-
- IF: you want to count instances of EACH class, you must
- declare the static variable for each class .. no getting
- around this. However, you CAN automate the increment
- and decrement somewhat:
-
- class Counter {
- virtual int* counter()=0;
- public:
- Counter() { (*counter())++; }
- ~Counter() { (*counter())--; }
- int TellCount()const {return *counter();}
- };
-
- class X :
- public virtual Counter { // (1)
- static int count; // (2) must declare
- int * counter() {return &count;} // (3)
- ...
- };
-
- and lines 1-3 could be made a macro:
-
- #define autocount public virtual Counter { static ....
-
- so
-
- class X : autocount
- ...
- };
-
- (Yuk)
- (PS: I havent tested this)
- Note the use of a private virtual function here :-)
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-