Jump to content
  • Advertisement
Sign in to follow this  
Crazy Chicken

Darn Abstract Interfaces !!!!

This topic is 5413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Diamond problem, use virtual inheritance.

#include <iostream>
using namespace std;

// Interface 1
class CInterface
{
public:
virtual void Iam() = 0;
};
// Interface 2
class CInterface2 : virtual public CInterface
{
public:
virtual void Iam2() = 0;
};
// Concrete 1
class CConcrete : virtual public CInterface
{
public:
void Iam() { cout << "I am CConcrete" << endl; }
};
// Concrete 2
class CConcrete2 : public CConcrete, public CInterface2
{
public:

void Iam2() { cout << "I am CConcrete2" << endl; }
};
// Main
void main()
{
CInterface* pI = new CConcrete();
CInterface2* pI2 = new CConcrete2();

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


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!