Nathan Sidwell nathan@acm.org
Sun Jan 31 23:58:00 GMT 1999

Sami Romdhani wrote:
> class Base {};
> class Derived : public Base {};
> int main() {
>  Base* pb = new Derived;
>  Derived* pd = dynamic_cast<Derived*>(pb);
> }
> The compiler error I get is :
> cannot dynamic_cast `pb' (of type `class Base *') to type `class Derived *'

The compiler is correct. From the CD2, [expr.dynamic.cast] (5.2.7) clause 6
applies, as none of the previous are true. That clause is a `shall' clause
specifying that pb must be a pointer to a polymorphic class.

[class.virtual] (10.3), says that a polymophic class is one which contains or
inherits a virtual function.

Since Base neither declares or inherits a virtual member function, it is not
polymorphic. Therefore the dynamic_cast is not permitted.

You need to insert (something like)
	virtual ~Base(); 
into Base

Dr Nathan Sidwell :: Computer Science Department :: Bristol University
      You can up the bandwidth, but you can't up the speed of light      
nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk

More information about the Gcc mailing list