I need to declare finalizing method finalize()
for all descendants of the base class Base
, that should be called during destruction, and my intent was to call pure virtual void Base::finalize() = 0
from the ~Base()
, but c++ forbids such a thing. So my question is
How can we oblige descendants to do some finalizing work in right and preliminary defined way?
That code cannot be compiled:
#include <QDebug>
class Base {
public:
Base(){}
virtual ~Base(){
qDebug("deleting b");
finalize();
}
virtual void finalize() = 0;
};
class A : public Base
{
public:
A(){}
~A(){}
void finalize(){qDebug("called finalize in a");}
};
int main(int argc, char *argv[])
{
Base *b = new A;
delete b;
}
If I make Base::finalize()
not pure virtual, it is called from ~Base()
without dispatching to child since it have been already destructed.
I can call finalize() from child's destructor but question is how to force to do that. In other words my question is: is it possible to oblige people who will write descendants of the Base class to use finalizing method, well, in another way than commenting it in a documentation? :)
See Question&Answers more detail:os