class Foo { public: Foo () { } void unref () { delete this; } protected: ~Foo () { } }; class Bar : public Foo { }; int main() { Foo* f = new Foo (); f->unref (); f = new Bar (); f->unref (); }