Dynamic casting does not appear to be working at all

Martin von Loewis martin@mira.isdn.cs.tu-berlin.de
Wed Sep 9 03:45:00 GMT 1998


> A dynamic_cast<> from a base class to a derived class produces a compile
> time error.

Which compiler version? Which source code? On egcs 2.92.02, the
program below compiles fine. Please note that you cannot dynamic_cast
objects, you need to use pointers or similar.

struct A{
  virtual ~A();
};

struct B:A{
};

A::~A(){}

int main()
{
  A *a = new A;
  B *b = dynamic_cast<B*>(a);
}


> Also calling a method via a member pointer which is typedef'd as
> taking a base class reference to A, but is in reality expecting a
> derived class references to B, but is passed a derived class
> reference to C, which is then dynamic_cast<B> works where it should
> fail.

I was not able to translate this description into C++. Can you give an
example as well?

Regards,
Martin



More information about the Gcc-bugs mailing list