c++ - Is there any Dynamic Binding During Deinitialization idiom -


aka: there "calling virtuals during deinitialization" idiom

i cleaning old code , need fix cases virtual methods called in constructors , destructors. don't know code base , huge. major rewrite not option.

the fix constructors simple. moved virtual calls static create template , made constructors protected. needed compile , change location causing errors use create template. minimal chance regressions. there no analog destructors.

how 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 not call derived::deinit (only prints "base"). need fix kind of issues.

working example code

this quite tricky, destructors called automatically on leaving scopes, whether normal flow, break, continue, return or throw. that's why can't pass arguments destructor.

the straightforward solution call derived::deinit derived::~derived. has additional benefit of still having derived members available.

another create own smart pointer class, calls t::deinit before t::~t. prevent being bypassed, return smart pointer create.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -