#include #include using namespace std; class base { public: base() {f();} //always calls base::f virtual ~base() {f();} //always calls base::f virtual void f() const {cout << "base::f\n";} void g() const {f();} //doesn't necessarily call base::f }; class derived: public base { public: derived(): base() {f();} //always calls derived::f ~derived() {f();} //always calls derived::f void f() const {cout << "derived::f\n";} }; int main() { base b; b.g(); derived d; d.g(); return EXIT_SUCCESS; }