Por que a herança protegida faz com que o dynamic_cast falhe?
Eu mudei minha classe base C ++ para serprotected
herança e meudynamic_cast
(s) parou de funcionar.
Por que mudar a herança paraprotected
mudar o comportamento dedynamic_cast
?
struct Base {
static Base *lookupDerived(); // Actually returns a Derived * object.
};
struct Derived : protected /* Switch this to public to get it working */ Base {
static void test() {
Base *base = lookupDerived();
if (dynamic_cast<Derived *>(base)) {
std::cout << "It worked (we must be using public inheritance)." << std::endl;
} else {
std::cout << "It failed (we must be using protected inheritance)." << std::endl;
}
};