This code returns me an error whenever I try to run this code. Can some one please help me.
struct m
{
virtual int s( )
{
return 1;
}
};
struct n : public m
{
int s( )
{
return 2;
}
};
int o( )
{
n* p=new m;
m* q=dynamic_cast<p>;
return q->s( );
}
These C++ cast operators should be used as
In your case,
BTW, are you confusing the role of
mandn?n* p = new mis a syntax error because a base class instance cannot be implicitly converted to a derived class instance. In fact, base → derived is the situation where you actually needdynamic_cast, not the other way around (no casting is needed).Also, consider giving meaningful names to objects.