I have a template, which adds a specific ability to a class:
template <Class ReceiverOfAbility>
class Able : public ReceiverOfAbility, SomeOtherClass
{
...
}
Then I have a standard tree-like inheritance:
class Base
{};
class Derived1 : public Base
{};
class Derived2 : public Derived1
{};
...
But somewhere in the tree there is a “backwards branch”
class Derived2WithAbility : public Able<Derived2>
{};
Suppose I have:
Base* basePointer = new DerivedWithAbility;
How can I cast basePointer to SomeOtherClass at runtime? dynamic_cast does not work. The problem is that there will be a lot of derived classes and I do not know in forward where in the tree does the class inherit the ability. I would need to search in the tree for the place where the inheritance from Able occurs.
Any ideas?
EDIT:
I tried to
dynamic_cast<SomeOtherClass*>(dynamic_cast<Derived2WithAbility*>(basePointer))
But I get:
error: ‘SomeOtherClass’ is an inaccessible base of ‘Derived2WithAbility’
Any Idea why?
SomeOtherClassis a private base class ofAble<whatever>. Fordynamic_castto work (i.e. to be able to cast toSomeOtherClasssuccessfully), it must be a public base.