I am trying to downcast an upcasted pointer, in which the inheritance hierarchy looks lie the following:
A : public Actor
Actor : private ActorImpl
When I save the upcasted pointer of an instance of class A as a pointer to ActorImpl and then when I try to downcast the pointer later back into A, i fail as the pointer after the dynamic cast is a nullptr. I have commented to place where the assertion fails. But the values of std::is_base_of gives me idea that they should not fail.
A* downcast<A>(ActorImpl *ai)
{
assert(ai != nullptr);
A *downcasted = nullptr;
if (std::is_base_of<Actor, A>::value)
{
bool grandparent = std::is_base_of<ActorImpl, A>::value;
assert(grandparent);
bool parent = std::is_base_of<ActorImpl, Actor>::value;
assert(parent);
//this assertion fails even though the A has Actor as parent and ActorImpl as grandparent
Actor *downcastedtmp = dynamic_cast<Actor *>(ai);
assert(downcastedtmp);
downcasted = dynamic_cast<A *>(downcastedtmp);
assert(downcasted);
}
else
{
bool bb = std::is_base_of<ActorImpl, A>::value;
assert(bb);
downcasted = dynamic_cast<A *>(ai);
assert(downcasted);
}
return downcasted;
What is the reason that this assertion fails? Than you.