(new) Abstract Class Problem!
Hi,
I've created a class called Canvas that simply wraps around a DirectDraw surface and does error checking, etc. I also have a Painter16 and Painter32 class that derives from the Canvas class and will do custom 16bit/32bit drawing on the encapsulated surface within the base Canvas class.
These two classes, Painter16 and Painter32, are also dervied from a IPainter class that has nothing other than some pure virtual functions and are implemented in the two dervied classes.
In my application I have a IPainter* pointer and I check if the display is in 16bit or 32bit mode and create either a Painter16/32 instance and cast it to my IPainter* pointer.
Now if I do
Painter16* ptr = new Painter16( ... );
IPainter* iptr = ( ( IPainter* )( ptr ) );
delete iptr;
it works fine, but doing this
IPainter* iptr = new Painter16( ... )
works right up until I do this
delete iptr;
Does anyone have any ideas why this is happening?
Thanks,
Michael
Hi,
The destructor in the Canvas class is virtual, but the IPainter class does not have anything apart from the the virtual functions.
I also tried a virtual destructor in the IPainter class, but no luck!
Regards,
Michael
The destructor in the Canvas class is virtual, but the IPainter class does not have anything apart from the the virtual functions.
I also tried a virtual destructor in the IPainter class, but no luck!
Regards,
Michael
Quote:Original post by spudgun2004Painter16* ptr = new Painter16( ... );IPainter* iptr = ( ( IPainter* )( ptr ) );delete iptr;
if Painter16 is a sub-type of IPainter then there is no need to cast from Painter16 to IPainter.
Perhaps you should post your types.
you do have to dynamically cast the interface ptr back to the type it was created as.
so Painter16* pPainter = dynamic_Cast<Painter16*>(iPtr);
then
delete pPainter;
that'll do the trick for you!!!
CHeers
Chris
so Painter16* pPainter = dynamic_Cast<Painter16*>(iPtr);
then
delete pPainter;
that'll do the trick for you!!!
CHeers
Chris
Quote:you do have to dynamically cast the interface ptr back to the type it was created as.
No you don't... You can delete a derived class through a pointer to the base class just fine. Ofcourse, if your destructor is not virtual, the derived class' destructor will never be called.
Now, you never told us what the error was. You just said it 'works fine until'. Is there a compile time error? Does it crash at runtime? What was the error?
[edit: destructor]
[Edited by - Magmai Kai Holmlor on September 2, 2004 11:40:46 PM]
Umm Deyja
you can't have a virtual constructor:) Nice try, wise guy:)
A virtual destructor on the other hand.... now that would do the trick
Cheers
Chris
you can't have a virtual constructor:) Nice try, wise guy:)
A virtual destructor on the other hand.... now that would do the trick
Cheers
Chris
Quote:you can't have a virtual constructor:) Nice try, wise guy:)
I like how you rebutted my typo, but neglected to mention that you were wrong in the first place.
I'm not so sure it was a typo and my original post was correct, it would fix the problem:)
Please don't get angry over silly things:) My post was correct and you called it wrong, and I picked apart your post.
Lets call it even!
Cheers
Chris
Please don't get angry over silly things:) My post was correct and you called it wrong, and I picked apart your post.
Lets call it even!
Cheers
Chris
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement