home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.object:2943 comp.lang.c++:11206
- Newsgroups: comp.object,comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!manuel!csis!oscar
- From: oscar@csis.dit.csiro.au (Oscar Bosman)
- Subject: Multiple Inheritance: `mix-in' classes and derived types
- Message-ID: <1992Jul21.030023.4220@csis.dit.csiro.au>
- Organization: CSIRO Division of Information Technology
- Date: Tue, 21 Jul 1992 03:00:23 GMT
- Lines: 74
-
-
- I have a design based upon some abstract classes (A, B, C, D, ... in the
- diagram below) which I would like to mix and match to make my concrete
- classes (L, M, N in the diagram). So far this a standard 'mix-in'
- design.
-
- A B C D E ...
- |\ /| /|
- | \/ | / |
- | /\ | / |
- |/ \|/ |
- L M N ...
-
- The problem occurs when I wish declare a function which operates on
- all classes derived from both `A' and `C'. Ideally I'd like to say
- something like (in modified C++ syntax):
-
- foo(A & C my_obj) { ... }
-
- where `&' indicates a kind of intersection of types. I can see two
- ways to do fudge this, neither of which I like.
-
- 1. I could declare all the combinations of the `mix-in' classes (A_B,
- A_C, B_C, ..., A_B_C, ...) and insist that concrete classes are
- derived from the appropriate `combination' class. But this is counter-
- intuitive and makes it tedious to add new mix-ins.
-
- 2. I could declare the argument to be of one type (say `A') and use
- run-time type checking to check that the argument is also of the other
- type before down-casting to a `combination' class. In Sather this
- might look like this (I don't think that the proposed type-safe down-
- casting for C++ gives the correct behaviour because I don't want to
- type-check against A_C).
-
- class R is
-
- foo(a:$A) is
- assert(pre) a.of_type(C::type) end;
- a_c: $A_C := a;
-
- -- do something with the `A' features of a_c
- -- do something with the `C' features of a_c
- end;
-
- end;
-
- The problem here is that the compile should be able to type-check the
- argument.
-
- There is a proposal to add `type adoption' to Sather 1.0 which would
- go some way to solving my problems with solution 1 above. As I
- understand it, it would go something like this:
-
- class A_C is
- adopts L; // A_C is now a superclass of L
- adopts M; // A_C is also superclass of M
-
- foo(a:$A_C) is
- -- do something with the `A' features of a
- -- do something with the `C' features of a
- end;
-
- end;
-
-
- Does anyone have some experience with using mix-ins in this way. I'm
- paticularly interested in solutions implemented in C++.
-
- Oscar.
- --
- Oscar.Bosman@csis.dit.csiro.au Centre for Spatial Information Systems
- CSIRO Div. Information Technology
- phone: +61-6-2750912 GPO Box 664
- fax: +61-6-2571052 Canberra, 2601. AUSTRALIA.
-