Archived

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

AlexM

derived class destruct

Recommended Posts

AlexM    122
i have a base class and a bunch of classes derived from it. Then my program basically calls the base class functions, but the class could in fact be any of the derived classes. the program doesn''t care and the function calls go through ok, except the destructor. i have to explicitely create a CleanUp function for each class and call it. ex: class baseclass { func1; baseclass(); CleanUp(); ~baseclass(); }; class c1 : public baseclass { func1; c1(); CleanUp(); ~c1(); } class c2 : public baseclass { func1; c2(); CleanUp(); ~c2(); } baseclass *c = new c2; c->func1(); //works perfectly, calls c2 func1 but i can''t do this: delete c; //it calls the baseclass destructor, not c2 destr i have to explicity: c->CleanUp(); //c2 CleanUp func is called... delete c; Any way to get around this? I don''t want to have to worry about calling CleanUp(). And yes, i tried putting a call to overridable CleanUp in baseclass destructor - it calls the baseclass CleanUp only!

Share this post


Link to post
Share on other sites
AlekM    100
Are your Destructors defined as virtual?



class CBaseClass
{
public:
CBaseClass();
virtual ~CBaseClass();
};

class CDerivedClass
{
public:
CDerivedClass();
virtual ~CDerivedClass();
};

Share this post


Link to post
Share on other sites