473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

c++ interfaces

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++?
Sep 22 '05 #1
6 1369
* Ben Smith:

Is there something I am missing here?


Virtual inheritance.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 22 '05 #2

Ben Smith wrote:
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)

Sep 22 '05 #3
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.

Sep 22 '05 #4
> 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
Sep 22 '05 #5
On Thu, 22 Sep 2005 07:10:39 GMT, al***@start.no (Alf P. Steinbach)
wrote:
* Ben Smith:

Is there something I am missing here?


Virtual inheritance.


brevity is wit.
Sep 23 '05 #6
On 22 Sep 2005 00:28:19 -0700, "John Fullman" <jo**********@gmail.com> wrote:

Ben Smith wrote:
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)


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.
Sep 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
10
by: hyperboreean | last post by:
Hi, Probably it has been asked before, but I'll still ask. Why doesn't python provide interfaces trough its standard library? Or it was ever proposed to be included in the language? Zope's...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.