fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4.  
  5. class Base {
  6. std::string data;
  7. protected:
  8. std::vector< std::function<void()> > steps;
  9. public:
  10. Base() : data("qq")
  11. , steps{
  12. [](){ std::cout << "BaseMethod1()" << std::endl; }
  13. , [](){ std::cout << "BaseMethod2()" << std::endl; }
  14. , [](){ std::cout << "BaseMethod3()" << std::endl; }
  15. , [this](){ std::cout << "data: " << data << std::endl; }
  16. }
  17. { }
  18. };
  19.  
  20. struct A final : Base {
  21. A() {
  22. steps.insert(
  23. steps.begin() + 2,
  24. [](){ std::cout << "A_Specific_Operations()" << std::endl; }
  25. );
  26. }
  27.  
  28. void doSomething() {
  29. for( auto&& step : steps ) step();
  30. }
  31. };
  32.  
  33. struct B final : Base {
  34. B() {
  35. steps.insert(
  36. steps.begin() + 1,
  37. [](){ std::cout << "B_Specific_Operations()" << std::endl; }
  38. );
  39. }
  40.  
  41. void doSomething() {
  42. for( auto&& step : steps ) step();
  43. }
  44. };
  45.  
  46.  
  47. int main() {
  48.  
  49. using namespace std;
  50.  
  51. A().doSomething();
  52. cout << endl;
  53. B().doSomething();
  54.  
  55. }
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
BaseMethod1()
BaseMethod2()
A_Specific_Operations()
BaseMethod3()
data: qq

BaseMethod1()
B_Specific_Operations()
BaseMethod2()
BaseMethod3()
data: qq