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: <1993Jan11.051953.9229@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> <86431@ut-emx.uucp>
- Date: Mon, 11 Jan 1993 05:19:53 GMT
- Lines: 83
-
- In article <86431@ut-emx.uucp> jamshid@emx.utexas.edu writes:
- >I quote a few different articles below -- watch the attrib lines.
- >
- >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.
- >
- >maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) answers in article <1993Jan5.051940.28930@ucc.su.OZ.AU>
- >| 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.
- >|[code deleted]
- >
- >Not allowing one to derive from the "working" class is too serious a
- >restriction, IMHO.
-
- Not if you follow the paradigm consistently.
- The "working" classes will always be derived 'at the last minute'
- and preferably anonymously.
-
- For each class A, you have a class workA. If you want
- to derive from 'workA' dont-just derive from 'A'. I cant
- see any restriction here, just boring typing maintaining
- two sets of classes.
- >|
- >| class Counter {
- >| virtual int* counter()=0;
- >| public:
- >| Counter() { (*counter())++; }
- >| ~Counter() { (*counter())--; }
- >| int TellCount()const {return *counter();}
- >| };
- >
- >Famous last words :-). I do agree it would be a yucky solution, but
- >fortunately it just doesn't work. The Counter ctor/dtor are calling
- >a virtual function (and a pure one at that) -- make sure your
- >compiler sternly warns about this.
-
- You're right!
- >
- > class A : public KeyCount<A> {/*...*/};
- >
- >That's it. Comments?
-
- It doesnt work :-)
-
- class A : public KeyCount<A> { ... }
- class Z : public A, public Keycount<Z>
- {
- A a;
- };
- Z z;
- A a;
-
- There is one 'A' object and one 'Z' object agree?
-
- But the counts show:
-
- Z==1; // correct
- A==3; // woops!
-
- To get this right one can follow a strict paradigm:
-
- class workA : public A, public KeyCount<A> {};
- workA a;
- class Z : public A { A a; };
- class workZ : public Z, public KeyCount<Z> {};
- workZ z;
-
- // always allocate a 'work' class to construct a whole
- // object
- // never contain or derive from this class
-
- Counts are now: A==1, Z==1. Correct.
-
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-