this is my first question here :)
I know that I should not check for object type but instead use dynamic_cast, but that would not solve my problem.
I have class called Extension and interfaces called IExtendable and IInitializable, IUpdatable, ILoadable, IDrawable (the last four are basicly the same). If Extension implements IExtendable interface, it can extend itself with different Extension objects.
The problem is that I want to allow the Extension which implements IExtendable to extend only with Extension that implements the same interfaces as the original Extension.
You probably don't unerstand that mess so I try to explain it with code:
class IExtendable
{
public:
IExtendable(void);
void AddExtension(Extension*);
void RemoveExtensionByID(unsigned int);
vector<Extension*>* GetExtensionPtr(){return &extensions;};
private:
vector<Extension*> extensions;
};
class IUpdatable
{
public:
IUpdatable(void);
~IUpdatable(void);
virtual void Update();
};
class Extension
{
public:
Extension(void);
virtual ~Extension(void);
void Enable(){enabled=true;};
void Disable(){enabled=false;};
unsigned int GetIndex(){return ID;};
private:
bool enabled;
unsigned int ID;
static unsigned int _indexID;
};
Now imagine the case that I create Extension like this:
class MyExtension : public Extension, public IExtendable, public IUpdatable, public IDrawable
{
public:
MyExtension(void);
virtual ~MyExtension(void);
virtual void AddExtension(Extension*);
virtual void Update();
virtual void Draw();
};
And I want to allow this class to extend itself only with Extensions that implements the same interfaces (or less). For example I want it to be able to take Extension which implements IUpdatable; or both IUpdatable and IDrawable; but e.g. not Extension which implements ILoadable. I want to do this because when e.g. Update() will be called on some Extension which implements IExtendable and IUpdateable, it will be also called on these Extensions which extends this Extension.
So when I'm adding some Extension to Extension which implements IExtendable and some of the IUpdatable, ILoadable... I'm forced to check if Extension that is going to be add implements these interfaces too. So In the IExtendable::AddExtension(Extension*) I would need to do something like this:
void IExtendable::AddExtension(Extension* pEx)
{
bool ok = true;
// check wheather this extension can take pEx
// do this with every interface
if ((*pEx is IUpdatable) && (*this is_not IUpdatable))
ok = false;
if (ok) this->extensions.push_back(pEx);
}
But how? Any ideas what would be the best solution? I don't want to use dynamic_cast and see if it returns null... thanks