PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > multiple inheritance \ questionable design
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
multiple inheritance \ questionable design

Réponse
 
LinkBack Outils de la discussion
Vieux 18/10/2007, 15h54   #1 (permalink)
Andrew
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut multiple inheritance \ questionable design

Hi,

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.

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.

Another way would be that IInterface2 and IInterface3 be separate
interfaces in the way that they do not inherit from the base
IInterface. But
the reason they do inherit is that I want to be able to use
IInterface2 and
IInterface3 as an IInterface too or else said, given an object
pointing to
IInterface2 i want to be able to use IInterface methods.

COM for example uses the QueryInteface method to obtain the other
interfaces
of an object but I do not want to use COM for this.

The client knows which type of object does it have given an
IInterface*, by
using an get_type() method from IInterface. (i did not include it in
order
to look simpler). It will then cast to the appropiate interface
(another
option would be using the dynamic_cast with RTTI on but i thought it's
simpler with get_type() ).

  Réponse avec citation
Vieux 19/10/2007, 08h46   #2 (permalink)
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
Vieux 19/10/2007, 12h17   #3 (permalink)
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: multiple inheritance \ questionable design

On 2007-10-19 03:46:39 -0400, James Kanze <james.kanze@gmail.com> said:

>
>> 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?


It's a busybody compiler warning.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h56.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09122 seconds with 11 queries