In the below code, why does the compiler agree on casting a base class to a derived class knowing the object is purely a base one? How can print2() be called although it's a derived function? Can you tell me please what happens exactly from the point of view of a compiler/memory manager?
#include <iostream>
using namespace std;
class Base {
public :
virtual void print(){
cout << "Hello from Base" << endl;
}
};
class Derived : public Base {
public:
void print(){
cout << "Hello from Derived" << endl;
}
void print2(){
cout << "Print2" << endl;
}
};
int main()
{
Base * b = new Base();
Derived * d = static_cast<Derived *>(b);
d -> print2();
}