Darn Abstract Interfaces !!!!

Started by
6 comments, last by Crazy Chicken 19 years, 7 months ago
Trying to develop a wrapper based on abstract interfaces and I ran into problems when using multiple inheritance.

#include <iostream>
using namespace std;

// Interface 1
class CInterface
{
public:
	virtual void Iam() = 0;
};
// Interface 2
class CInterface2 : public CInterface
{
public:
	virtual void Iam2() = 0;
};
// Concrete 1
class CConcrete : public CInterface
{
public:
	void Iam() { cout << "I am CConcrete" << endl; }
};
// Concrete 2
class CConcrete2 : public CConcrete, public CInterface2
{
public:
        // WHY DO I NEED TO IMPLEMENT IT HERE !!!!
	void Iam() { CConcrete::Iam(); }
       
	void Iam2() { cout << "I am CConcrete2" << endl; }
};
// Main
void main()
{
 CInterface* pI = new CConcrete();
 CInterface2* pI2 = new CConcrete2();

 //pI->Iam();
 //pI2->Iam();
}


The problem is that class CConcrete2 needs to implement function IAm() but should it not have inheriated the function from class CConcrete? Any suggestions will be helpful. Thx Luke
Advertisement
Diamond problem, use virtual inheritance.
#include <iostream>using namespace std;// Interface 1class CInterface{public:	virtual void Iam() = 0;};// Interface 2class CInterface2 : virtual public CInterface{public:	virtual void Iam2() = 0;};// Concrete 1class CConcrete : virtual public CInterface{public:	void Iam() { cout << "I am CConcrete" << endl; }};// Concrete 2class CConcrete2 : public CConcrete, public CInterface2{public:       	void Iam2() { cout << "I am CConcrete2" << endl; }};// Mainvoid main(){ CInterface* pI = new CConcrete(); CInterface2* pI2 = new CConcrete2(); pI->Iam(); pI2->Iam(); delete pI; delete pI2;}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Is the diamond shape a design flaw or is used ofen in development?
Is it less of a problem because I am always only inheriting from one non pure abstract class?

[Edited by - Crazy Chicken on September 26, 2004 3:34:21 PM]
Well, the problem comes in with ambiguities. Specifically, without virtual inheritance you end up with two copies of the root class. So when you call one of it's methods, which gets called? The one from Concrete2? or the one from Interface2? Virtual inheritance avoids this by having only a single instance of the base class.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I am aware of the problem but my question is whether developing a wrapper based on virtual inheritance will be a problem in the long run? Is there a better to provide an interface an inheritance hierarchy?

Thx,
Luke
Quote:Original post by Crazy Chicken
Is the diamond shape a design flaw or is used ofen in development?


diamond shaped inheritance is not a design flaw its self, it depends on what try to model/design, I/O streams library has a diamond shaped inheritance.

Quote:Original post by Crazy Chicken
Is it less of a problem because I am always only inheriting from one non pure abstract class?


If you could explain what it is your trying to design then it will make easier to tell if your design is flawed.

note rule of thumb (actually take it as must) if you see the keyword virtual inside a class then make your destructor virtual aswell other-wise you'll end up with subtle problems.
It's certainly not the way I would do it...However, it does have it's applicability to certain situations. In the end, it really depends on your problem.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

I am trying to design a wrapper for a game engine based around a layer of abstraction (interfaces). As an example I want to create a concrete DXMesh class that is imlpemented with DirectX. There will be a class inherited from it, say SkinnedDXMesh. In order to keep the interface system I'd have a pure abstract class IMesh that is the super class of DXMesh, and a ISkinnedMesh inherited from IMesh that is a super class of skinnedDXMesh.

Basically if I've not made it not clear enough just replace these names in the example code I posted.

Interface - > IMesh
Interface2 - > ISkinnedMesh
Concrete - > DXMesh
Concrete2 - > SkinnedDXMesh

It is likely much of the wrapper will be based around this design...

thanks :)

This topic is closed to new replies.

Advertisement