Archived

This topic is now archived and is closed to further replies.

pcxmac

virtual destructors

Recommended Posts

the quicky : one of my base classes dtor's or destructors needs to be virtual class base { public : virtual ~base(); // i have it set like this in the class declaration } ~base::base() { delete(next); } class derived { public : ~derived() } when i try to delete derived it still goes to the base class's destructor, which makes sence, and i tried to do this : derived::~base(){ something }; but it came up with an error HELP ME PLEASE !!! dont read this unless your insanely patient heres my plan base class; midd class; uppr class; midd class; uppr class : public base , public midd class; uppr class uses the midd class to link more midd classes to it. and the way my object is set up i have to skip the midd classes destructor in order to delete everything orderly like. so upon delete uppr class it goes base class delete() { NILL }; skip midd class and go to uppr class's virtual overide delete() for midd. uppr::~midd() { { NILL }; }; uppr::~uppr() { delete((midd*)next); } next(midd)::~midd() { delete(next); // next will eventually become a pointer to the uppr class }; uppr::~midd() { NILL }; // from here the code drops back through the already traversed midds and back to uppr uppr is taken care of ! so how do i impliment a virtual destructor with out arguments ??? class midd { virtual ~midd(); ...}; class uppr : public base , public midd { ~uppr(); ~midd(); // does this work? }; ok and if someone could tell me how to use the metta message deal for this forum i would appreciate it, and it would make it easier to show my code. [edited by - pcxmac on July 27, 2003 6:26:50 AM] [edited by - pcxmac on July 27, 2003 9:48:29 AM]

Share this post


Link to post
Share on other sites
Any given class can have one, and only one, destructor. So you can''t redefine a base class''s destructor in a derived class. What you could do, however, would be to have a virtual function Cleanup(), which is called in the destructor, and can then be redefined in a derived class.

Share this post


Link to post
Share on other sites
The destructors of a class should only clean up that class. So, if you have a derived class that you need cleaned up, use it''s destructor. If your object model doesn''t support it, change it so it does.

The virtual keyword on destructors only makes sure that destructors in derived classes are called. You can''t "overload" a destructor.

Death of one is a tragedy, death of a million is just a statistic.

Share this post


Link to post
Share on other sites