Could someone explain this to me:
struct A {
virtual ~A() = default;
};
struct B : private A {
void foo() {
B* b = new B;
std::list<A*> list = {b};
B* back = dynamic_cast<B*>(list.back());
std::cout << back << std::endl;
}
};
int main() {
B b;
b.foo(); // nullptr
}
From what I understand, only B is aware that B is derived from A. foo() is a member function of B. So dynamic_casting from A* to B* should be fine inside B::foo(). So why is the result nullptr? Same thing goes if it is protected inheritance. Change the inheritance to public, and everything is fine. What's going on here?