fork download
  1. #include <iostream>
  2.  
  3. struct Base
  4. {
  5. void f() const
  6. {
  7. pre_f();
  8. std::cout << "f() stuff\n";
  9. post_f();
  10. }
  11.  
  12. virtual ~Base() {}
  13.  
  14. private:
  15. virtual void pre_f() const = 0;
  16. virtual void post_f() const = 0;
  17. };
  18.  
  19. struct Derived : Base
  20. {
  21. private:
  22. virtual void pre_f() const override
  23. {
  24. std::cout << "pre f() stuff\n";
  25. }
  26.  
  27. virtual void post_f() const override
  28. {
  29. std::cout << "post f() stuff\n";
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. Derived d;
  36. d.f();
  37. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
pre f() stuff
f() stuff
post f() stuff