This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: dynamic_cast


Hi Sebastian,

 You can only use RTTI on instances of a class with a
virtual table, that is has at least one virtual function,
even if you don't actually need one. You should not
delete an instance of a polymorphic class without a
virtual destructor through a pointer or reference to
an instance of a base class, as this will only ensure
the base class destructor is run. Check Meyers effective
C++ books.

Yours sincerely,

bjorn

Sebastian Huber wrote:
> 
> Hello,
> why does the following code fragment not work?
> 
> class Storable { };
> 
> class Component : virtual public Storable { };
> 
> int main()
> {
>         Storable* s = new Component();
>         Component* c = dynamic_cast<Component*>( s);
>         delete s;
> 
>         return 0;
> }
> 
> This code is very similar to an example of Bjarne Stoupstrup.
> I use g++ version 2.95.3.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]