Aka: Is there any "Calling Virtuals During Deinitialization" idiom
I am cleaning up some old code and need to fix cases where virtual methods are called in constructors and destructors. I don't know the code base and it is huge. Major rewrite is not an option.
The fix for constructors was simple. I moved the virtual calls to a static Create
template and made all the constructors protected. Then all I needed to do was to compile and change all location causing errors to use the Create
template. Minimal chance for regressions. However there is no analog to this for destructors.
How would you solve this?
Example code
#include <iostream>
class Base
{
public:
virtual ~Base()
{
DeInit();
}
protected:
virtual void DeInit()
{
std::cout << "Base" << std::endl;
}
};
class Derived : public Base
{
protected:
virtual void DeInit() override
{
std::cout << "Derived" << std::endl;
Base::DeInit();
}
};
int main()
{
Derived d;
}
This code does not call Derived::DeInit
(only prints "Base"). I need to fix this kind of issues.