#include <iostream>
using namespace std;
struct Base {
void doBase() {
cout << "bar" << endl;
}
};
struct Derived : public Base {
void doBar() {
cout << "bar" << endl;
}
};
int main()
{
Base b;
Base* b_ptr = &b;
Derived* d_ptr = static_cast<Derived*>(b_ptr);
d_ptr->doBar(); //Why there is no compile error or runtime error?
return 0;
}
The output of this program is bar\n
.
d_ptr
is in fact pointing to a Base class while it 's calling a derived class member function.
Is it a undefined behaviour or something else?