Jump to content
  • Advertisement
Sign in to follow this  
spudgun2004

(new) Abstract Class Problem!

This topic is 5136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Quote:
Original post by spudgun2004

Painter16* 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!