c++ interfaces

B

Ben Smith

In this sample inheritance hierarchy:

class I1
{
public:
virtual void a() = 0;
virtual void b() = 0;
};

class I2 : public I1
{
public:
virtual void c() = 0;
};

class AC1 : public I1
{
public:
virtual void a() { printf( "AC1::a()\n" ); }
};

class C1 : public AC1, public I2
{
public:
// using AC1::a; // *
// virtual void a() { AC1::a(); } // **
virtual void b() { printf( "C1::b()\n" ); }
virtual void c() { printf( "C1::c()\n" ); }
};

I1, I2 are interfaces, i.e. pure abstract base classes.
AC1 is an abstract class.
C1, in my opinion, should be a concrete class, but is not.

The problem: C1 does not implement method a(), inherited from its base
class
I2. In this case the C++ paradigm of using a pure abstract base class
does
not mimic exactly the functionality of a java interface. In java, the
fact
that C1's base class AC1 implements a() is enough.

As far as I can tell, the only way to make this work as expected is to
manually delegate any missing methods; for example, the **-marked
method
definition above. The * statement is enough to disambiguate between
AC1::a()
and I2::a(), but is not enough to consider AC1::a() an implementation
of
I2::a().

Is there something I am missing here? Is there no better way to
implement
(pun intended) interfaces in C++?
 
J

John Fullman

Ben said:
class C1 : public AC1, public I2

Your problem is that you have 2 instances of I1 in your C1 class...
Remeber that C++ doesn't really have "interfaces" just classes with
potentially missing implementation. (abstract)
 
P

Pete C

You have two I1 objects in your C1 class - but you have only overridden
a() for one of them. Read up on virtual inheritance, it's often what
you want if you're trying to implement a Java-style interface.

If you change
class I2 : public I1
to
class I2 : virtual public I1

and
class AC1 : public I1
to
class AC1 : virtual public I1

Then your code will do what (I think) you want.
 
B

benben

As far as I can tell, the only way to make this work as expected is to
manually delegate any missing methods; for example, the **-marked
method
definition above. The * statement is enough to disambiguate between
AC1::a()
and I2::a(), but is not enough to consider AC1::a() an implementation
of
I2::a().

What do you mean the using declarative is not enough to consider AC1::a() an
implementation of I2::a()?
* AC1::a() really doesn't implement I2::a()
* C1::a() implements I2::a()

In addtion, I would urge you to provide virtual destructors for all the
abstract classes.

Ben
 
D

Dave Rahardja

Your problem is that you have 2 instances of I1 in your C1 class...
Remeber that C++ doesn't really have "interfaces" just classes with
potentially missing implementation. (abstract)

Although this is true, the behavior of interfaces can be emulated in C++ using
classes containing only pure virtual member function declarations, and using
virtual inheritance. I've successfully deployed that pattern in several
reusable code libraries.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top