Disable local class variable
class B { public: virtual ~B(){} }; class D : public B { public: D(){} protected: virtual ~D(){} }; int main() { D d; // Illegal. B *pD = new D; // Ok. delete pD; pD = NULL; return 0; }
As you see, the key is to disable create local variable by prtected access in class D, and call the dtor through polymorphism.