ranadhir27
April 17th, 2002, 01:21 AM
class Pet{
public:
virtual void eat(){cout<<"Pet eating"<<endl;}
};
class Dog:public Pet{
public:
void eat(){cout<<"Dog eating"<<endl;}
virtual void sleep(){cout<<"Dog sleeping"<<endl;}
};
int main()
{
Pet* qq=new Pet;
//Dog* DD=(Dog*)qq;[1]
Dog* DD=dynamic_cast<Dog*>(qq);[2]
DD->eat();[3]
}
In the above the compilations for both static cast and dynamic cast go through.But while static cast executes [3] successfully,dynamic casting causes segmentation fault at [3].How do we explain this?
public:
virtual void eat(){cout<<"Pet eating"<<endl;}
};
class Dog:public Pet{
public:
void eat(){cout<<"Dog eating"<<endl;}
virtual void sleep(){cout<<"Dog sleeping"<<endl;}
};
int main()
{
Pet* qq=new Pet;
//Dog* DD=(Dog*)qq;[1]
Dog* DD=dynamic_cast<Dog*>(qq);[2]
DD->eat();[3]
}
In the above the compilations for both static cast and dynamic cast go through.But while static cast executes [3] successfully,dynamic casting causes segmentation fault at [3].How do we explain this?