You can use static_cast
in your example.
But you must include both definitions of IInherit
and cDerived
for it to work. The compiler must see, that cDerived
inherits from IInherit
. Otherwise, it cannot decide that the static_cast
is indeed valid.
#include <vector>
struct R {};
struct B {};
struct D : public B {
R *getR() { return new R(); }
};
void f()
{
std::vector<B*> v;
v.push_back(new D());
D *d = static_cast<D*>(v[0]);
R *r = d->getR();
}