• ### What is your GameDev Story?

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

## Recommended Posts

Hi I'm making a 3D engine as a dll. To do so, I give in the SDK pure virtual functions as below :
//file iTexture.h
class iTexture
{
public:
iTexture()				{};
virtual ~iTexture(void) {};

virtual	int	GetHeight(void) = 0;
virtual	int	GetWidth(void) = 0;

/* .... */
};


And inside my dll, I do this :
//file cTexture.h
#include "iTexture.h"

class cTexture : public iTexture
{
friend	class	cTextureManager;

public:
cTexture(void);
virtual	~cTexture(void);

int	GetHeight(void);
int	GetWidth(void);

/* .... */
}


Problemes arises in derivated classes when I try to do something like that :
// file iStreamTexture.h

#include "iTexture.h"

class iStreamTexture : public iTexture
{
public:
iStreamTexture()	{};
~iStreamTexture()	{};

virtual	bool	LoadAVIStream(const char *filename) = 0;
/* ... */

}

// file cStreamTexture.h

#include "iStreamTexture.h"
#include "cTexture.h"

class cStreamTexture : public cTexture, iStreamTexture
{
public:
cStreamTexture();
virtual	~cStreamTexture();

/* ... */
}


Error C2259 happen (Unable to instanciate abstract classes). In a previous thread, Enigma gave me a solution to work around my problem. I want to know if it's the good solution to do such ? Let's look at other "dll engine" does as I do and they don't have this problem. Where am I wrong ?

##### Share on other sites
Make sure derived class has implementation of all pure virtual methods.

##### Share on other sites
Relying on interface is certainly a good thing, doing multiple inheritance is bad, very very bad :).

Your having a deadly diamond problem right now. You are trying to inherit from cTexture and IStreamTexture, both of which are depending on ITexture. I would recommand changing this and pulling that diamond out.

I think you can fix it if you try

using iStreamTexture::GetHeight;
using iStreamTexture::GetWidth;

in you cStreamTexture.

But this isn't the best solution. Also if you are working with dlls, you will need to export either your class (might not be in your best interest since you are working with interfaces anyway), or create a factory that will return you your interfaces. That factory will need to be exported (can be as simple as a CreateStreamTexture exported function).

Hope this helps,

##### Share on other sites
Quote:
 Original post by ShadxYour having a deadly diamond problem right now. You are trying to inherit from cTexture and IStreamTexture, both of which are depending on ITexture. I would recommand changing this and pulling that diamond out. I think you can fix it if you tryusing iStreamTexture::GetHeight;using iStreamTexture::GetWidth;in you cStreamTexture.

Here's what did :
	int	GetHeight(void)	{return cTexture::GetHeight();};	int	GetWidth(void)	{return cTexture::GetWidth();};

Quote:
 But this isn't the best solution. Also if you are working with dlls, you will need to export either your class (might not be in your best interest since you are working with interfaces anyway), or create a factory that will return you your interfaces. That factory will need to be exported (can be as simple as a CreateStreamTexture exported function).

I use a cTextureManager::CreateTexture(....) class inside the dll :)
As You said I have to get ride of this diamond problem...

##### Share on other sites
The problem is your use of non-virtual inheritance. With non-virtual inheritance a base class can be included multiple times in an inheritance hierarchy. For example your cStreamTexture inheritance hierarchy looks like:
    cStreamTexture      |        |      V        VcTexture     iStreamTexture    |               |    V               ViTexture       iTexture

Now cStreamTexture inherits two copies of iTexture. cTexture correctly overrides the pure virtual functions of one of those instances, but the pure virtual functions of the other instance are never overriden, causing cStreamTexture to be an abstract class.
By using virtual inheritance you resolve this problem and your inheritance hierarchy now looks like:
   cStreamTexture     |        |     V        VcTexture    iStreamTexture      |      |      V      V      iTexture

You now only have one copy of iTexture and cTexture overrides the pure virtual functions contained within.

This is actually one of the (relatively) few cases where multiple inheritance is probably a good solution to a problem. I certainly don't know of any nice equivalent solutions without using multiple inheritance.

It is important to note that this is a simple case of multiple inheritance. Since iTexture and iStreamTexture are abstract base classes many of the problems with general multiple inheritance cannot arise. In fact this form of virtual inheritance is so safe even Java implements it, via interfaces (no offense to Java programmers [lol]).

Enigma

##### Share on other sites
Enigma, you are my hero one more time :)
I found a "work around", but your solution is nice too.

many thx !

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 12
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015649
×