J
Juha Nieminen
This is a simplified version of the situation (the actual situation
is quite more complex, but when stripped to the bare minimum, it's
like this):
class BaseClass
{
public:
virtual void cleanup();
virtual ~BaseClass() { cleanup(); }
};
The cleanup() function is not only called when destroying the object
but also at other times during the lifetime of the object, and it's
usually called through a BaseClass pointer and sometimes implemented
in a derived class.
Naturally the problem is that the BaseClass destructor will not call
the implementation of cleanup() in the derived class, only its own.
This is a bummer, but I suppose it's like it should be (because,
basically, the derived part of the object "doesn't exist" anymore
when the destructor of the base class is executed).
The only clean solution to this is to explicitly call cleanup() in
the destructor of the derived class too.
Isn't there any way of automatically making the destructor of the
derived class call its own cleanup() function without having to write
it explicitly? The problem is that this is very easy to forget, it
obviously doesn't cause any compiler errors nor warnings and the
resulting behavior of the program can be quite confusing.
is quite more complex, but when stripped to the bare minimum, it's
like this):
class BaseClass
{
public:
virtual void cleanup();
virtual ~BaseClass() { cleanup(); }
};
The cleanup() function is not only called when destroying the object
but also at other times during the lifetime of the object, and it's
usually called through a BaseClass pointer and sometimes implemented
in a derived class.
Naturally the problem is that the BaseClass destructor will not call
the implementation of cleanup() in the derived class, only its own.
This is a bummer, but I suppose it's like it should be (because,
basically, the derived part of the object "doesn't exist" anymore
when the destructor of the base class is executed).
The only clean solution to this is to explicitly call cleanup() in
the destructor of the derived class too.
Isn't there any way of automatically making the destructor of the
derived class call its own cleanup() function without having to write
it explicitly? The problem is that this is very easy to forget, it
obviously doesn't cause any compiler errors nor warnings and the
resulting behavior of the program can be quite confusing.