Sign in to follow this  
iliak

Unity c++ class design in dll

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();

	bool	LoadAVIStream(const char *filename);
	
	/* ... */
}


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 this post


Link to post
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,

Shadx

Share this post


Link to post
Share on other sites
Quote:
Original post by Shadx
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.


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 this post


Link to post
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 V
cTexture iStreamTexture
| |
V V
iTexture 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 V
cTexture 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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this