Afficher un message
Vieux 19/10/2007, 08h46   #2
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: multiple inheritance \ questionable design

On Oct 18, 4:54 pm, Andrew <nites...@gmail.com> wrote:

> I need some advice regarding a C++ implementation.
> Suppose we have 3 interfaces, a base interface and two extending it:


> struct IInterface { virtual void m() = 0; };
> struct IInterface2 : virtual IInterface { virtual void m2() = 0; };
> struct IInterface3 : virtual IInterface { virtual void m3() = 0; };


> The interfaces are implemented like this:
> We implement the common base interface into an abstract class:


> struct BaseAbstract : virtual IInterface { virtual void m(); };


> and then implement the other 2 interfaces as 2 concrete classes:


> struct Concrete2 : BaseAbstract, IInterface2 { virtual void m2(); };
> struct Concrete3 : BaseAbstract, IInterface3 { virtual void m3(); };


> Now, this will give C4250 saying that the concrete classes
> inherits the method m() implemented by the BaseAbstract class
> via dominance. This can be overlooked but I was wondering if
> the design is OK.


What's C4250? This is a standard idiom, and has been at least
since Barton and Nackman, and I don't know how else you'd do it.
The rules for multiple and virtual inheritance were designed
expressedly for this.

> One other way is both Concrete classes implement for their own
> the IInterface methods and BaseAbstract won't be needed but
> that it's not OK I guess since it's duplicate code.


You definitly want the BaseAbstract. Depending on
circumstances, you might also provide an implementation in the
Concrete classes which just forwards to the BaseAbstract
implementation; in some cases, BaseAbstract might not even
inherit from IInterface, and the function might not be virtual.
But IMHO, such circumstances are the exception. The usual
solution is precisely what you've done.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

  Réponse avec citation
 
Page generated in 0,05192 seconds with 9 queries