Say I had two classes
class Bar{
public:
Bar()
~Bar()
}
class Foo:public Bar{
Foo()
~Foo
}
Now if I wanted to do the following:
int main(){
Bar * obj = new Foo();
Foo * obj2 = dynamic_cast<Foo*>(obj);
return 0;
}
I get an error that I cannot dynamic cast it because it's not polymorphic. I understand that it's because I don't have a virtual keyword anywhere in my base class (in my case a good situation would be in the destructor). But why do I need this virtual keyword to have this sort of true polymorphism? I understand it in the case of if I was trying to call a, say, display function, where the compiler/whatever is going at run time needs to figure out which display to call. But I'm not sure why it's required here. I'd love an explanation.
Thanks