multiple inheritance \ questionable design

A

Andrew

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() ).
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top