derived class destruct

Started by
1 comment, last by AlexM 22 years, 2 months ago
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!
Advertisement
Are your Destructors defined as virtual?



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

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

WHOAH, didn''t know you can do that... i feel terrible..
thanx

This topic is closed to new replies.

Advertisement