i know what virtual function and polymorphism is. i am bit confused in understanding my below code.
class base
{
public:
virtual void display()
{
cout <<"display base";
}
};
class derived :public base
{
public:
void display()
{
cout << "display derived";
}
};
int main()
{
derived *d = dynamic_cast<derived*>(new base); // this line is giving core dump
d->display();
return 0;
}
i am not sure why using dynamic_cast on base class is giving me segmentation fault. i should expect the output "derived display" as output. can someone please give me any leads on it and better explanation.