tags:

views:

86

answers:

3

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( );
}
+9  A: 
KennyTM
+3  A: 

Next time, please tell us what the error is!

I suppose you're saying that there is a compile error because you're not using dynamic_cast right? You probably meant to say

m* q=dynamic_cast<m *>(p); 

In general, dynamic_cast is the devil. Most implementations are insanely slow and might go as far as going string-based class name checks. Unless you really, absolutely need to use dynamic_cast, please use any other method available (such as static_cast).

EboMike
http://www.boost.org/doc/libs/1_43_0/libs/conversion/cast.htm has the best of both casts..
smerlin
A: 

Two problems, first you can't allocate an m and call it n in the first line of main. You have a syntatic error in the dynamic_cast. It is dynamic_cast<new type>(some var).

tim