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 > Question about interfaces
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question about interfaces

Réponse
 
LinkBack Outils de la discussion
Vieux 29/06/2008, 00h36   #1
sip.address@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Question about interfaces

Hi there,

When creating interfaces and implementations, the usual thing is doing
somethign like

class Interface {
public:
virtual void f() = 0;
virtual void g() = 0;
};

class Imp1 : public Interface {
public:
void f();
void g();
};

So we can have Interface* if = new Imp1; and so on..

But we could also use private inheritance:

class Imp1 {
protected:
void f();
void g();
};

class Interface : private Imp1 {
public:
void f() { Imp1::f(); }
...
};

What are the advantages/disadvantages of each? I believe in both cases
a level of indirection happens (through the virtual table or calling
the base class function in the second case). But probably today
compilers do some optimizations?

Any advise?

Thanks!

protected:
void f();
  Réponse avec citation
Vieux 29/06/2008, 01h42   #2
acehreli@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about interfaces

On Jun 28, 4:36pm, sip.addr...@gmail.com wrote:
[...]
> But we could also use private inheritance:
>
> class Imp1 {
> protected:
> void f();
> void g();
>
> };
>
> class Interface : private Imp1 {
> public:
> void f() { Imp1::f(); }
> ...
>
> };
>
> What are the advantages/disadvantages of each?


Normally, interfaces have more than one implementation as in Cat and
Dog inheriting from the Animal interface. So your private
implementation inheritance wouldn't work.

> I believe in both cases
> a level of indirection happens (through the virtual table or calling
> the base class function in the second case). But probably today
> compilers do some optimizations?


Sometimes, when the actual type of the object is known at compile time
I guess. Otherwise, in general the actual type is not known without
the use of virtual function table.

At a recent ACCU Silicon Valley talk, Ulrich Drepper mentioned how
indirect calls like C++'s virtual functions hurt the CPU's caching
schemes. Something to keep in mind, but I am not getting rid of my
virtual functions yet...

Ali
  Réponse avec citation
Vieux 29/06/2008, 17h22   #3
acehreli@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about interfaces

On Jun 29, 7:16am, "Daniel T." <danie...@earthlink.net> wrote:
> sip.addr...@gmail.com wrote:

[...]
> > But we could also use private inheritance:

>
> > class Imp1 {
> > protected:
> > void f();
> > void g();
> > };

>
> > class Interface : private Imp1 {
> > public:
> > void f() { Imp1::f(); }
> > ...
> > };

>
> Note, the above is effectively the same as composition.
>
> class Imp1 {
> public:
> void f();
> void g();
>
> };
>
> class Interface {
> Imp1 imp;
> public:
> void f() { imp.f(); }
>
> };
> > What are the advantages/disadvantages of each?

>
> The old "inheritance versus composition" question.


Not in this case though. It would be that question if the OP swapped
the class names; so to get to your example:

class Impl {
Interface interface;
public:
void f() { interface.f(); }

};

where Interface would not be an interface anymore.

> This isn't strictly a
> C++ question. Do a Google search on that term and you will get a
> plethora of opinions.
>
> > Any advise?

>
> "Favor object composition over class inheritance" (GoF)


Makes sense, but not in this question because there is an interface
that the OP is talking about. For the other types of class inheritance
like "a car HAS-AN engine", yes, that's composition.

Ali
  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 03h44.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11895 seconds with 11 queries