home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / object / 2954 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  3.7 KB

  1. Xref: sparky comp.object:2954 comp.lang.c++:11273
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!caen!kuhub.cc.ukans.edu!parsifal.umkc.edu!vax1.umkc.edu!kramasamy
  3. Newsgroups: comp.object,comp.lang.c++
  4. Subject: Re: Multiple Inheritance: `mix-in' classes and derived types
  5. Message-ID: <1992Jul21.135646.1@vax1.umkc.edu>
  6. From: kramasamy@vax1.umkc.edu
  7. Date: Tue, 21 Jul 1992 19:56:46 GMT
  8. Sender: root@parsifal.umkc.edu (Parsifal Administration)
  9. References: <1992Jul21.030023.4220@csis.dit.csiro.au>
  10. Organization: University of Missouri - Kansas City
  11. Lines: 107
  12.  
  13. In article <1992Jul21.030023.4220@csis.dit.csiro.au>, oscar@csis.dit.csiro.au (Oscar Bosman) writes:
  14. > I have a design based upon some abstract classes (A, B, C, D, ... in the
  15. > diagram below) which I would like to mix and match to make my concrete
  16. > classes (L, M, N in the diagram).  So far this a standard 'mix-in'
  17. > design.
  18. >         A B  C  D E ...
  19. >         |\  /|   /|
  20. >         | \/ |  / |
  21. >         | /\ | /  |
  22. >         |/  \|/   |
  23. >         L    M    N ...
  24. > The problem occurs when I wish declare a function which operates on
  25. > all classes derived from both `A' and `C'.  Ideally I'd like to say
  26. > something like (in modified C++ syntax):
  27. >      foo(A & C my_obj) { ... }
  28. > where `&' indicates a kind of intersection of types.  I can see two
  29. > ways to do fudge this, neither of which I like.
  30. > 1.  I could declare all the combinations of the `mix-in' classes (A_B,
  31. > A_C, B_C, ..., A_B_C, ...) and insist that concrete classes are
  32. > derived from the appropriate `combination' class.  But this is counter-
  33. > intuitive and makes it tedious to add new mix-ins.
  34. > 2.  I could declare the argument to be of one type (say `A') and use
  35. > run-time type checking to check that the argument is also of the other
  36. > type before down-casting to a `combination' class.  In Sather this
  37. > might look like this (I don't think that the proposed type-safe down-
  38. > casting for C++ gives the correct behaviour because I don't want to
  39. > type-check against A_C).
  40. > class R is
  41. >    
  42. >    foo(a:$A) is 
  43. >       assert(pre) a.of_type(C::type) end;
  44. >       a_c: $A_C := a;
  45. >       -- do something with the `A' features of a_c
  46. >       -- do something with the `C' features of a_c
  47. >    end;
  48. > end; 
  49. > The problem here is that the compile should be able to type-check the
  50. > argument. 
  51. > There is a proposal to add `type adoption' to Sather 1.0 which would
  52. > go some way to solving my problems with solution 1 above.  As I
  53. > understand it, it would go something like this:
  54. > class A_C is 
  55. >    adopts L;  // A_C is now a superclass of L
  56. >    adopts M;  // A_C is also superclass of M
  57. >    foo(a:$A_C) is 
  58. >       -- do something with the `A' features of a
  59. >       -- do something with the `C' features of a
  60. >    end;
  61. > end; 
  62. > Does anyone have some experience with using mix-ins in this way. I'm
  63. > paticularly interested in solutions implemented in C++.
  64. > Oscar.
  65. > -- 
  66. > Oscar.Bosman@csis.dit.csiro.au          Centre for Spatial Information Systems
  67. >                                         CSIRO Div. Information Technology
  68. > phone: +61-6-2750912                    GPO Box 664
  69. > fax:   +61-6-2571052                    Canberra, 2601.    AUSTRALIA.
  70.  
  71.     What about doing the following ?
  72.  
  73.     A  B  C  D  E 
  74.     \     /     /
  75.      \   /     /
  76.       \ /     /
  77.        X     /
  78.           / \   /
  79.          /   \ /
  80.     L     M
  81.  
  82.     Derive an abstract class X from 'A' and 'C' and all the classes which derive from both
  83. A and C will derive from X hereafter.
  84.  
  85.     Now your 'foo' function becomes something like the following
  86.  
  87.     foo( X &my_obj )
  88.     {
  89.         //do something specific to A
  90.         // do something specific to B
  91.     } 
  92.  
  93.  
  94.     I hope this helps you
  95.  
  96. -karthik
  97.  
  98. karthik@vax1.umkc.edu
  99.  
  100.  
  101.  
  102.