fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct TaskInterface {
  6. virtual void Do() = 0;
  7. virtual ~TaskInterface() {}
  8. };
  9.  
  10. template<class Impl>
  11. class TaskBase
  12. : public TaskInterface {
  13. virtual void Do() {
  14. DoDerivedImpl();
  15. }
  16. protected:
  17. void DoDerivedImpl() {
  18. static_cast<Impl*>(this)->DoImpl();
  19. }
  20.  
  21. void DoImpl() {
  22. // Issue a static_assert error here, that there's no appropriate overridden
  23. // implementation of DoImpl() available:
  24. static_assert(static_cast<Impl*>(this)->DoImpl != TaskBase<Impl>::DoImpl, "TaskBase requires an appropriate implementation of DoImpl()");
  25. }
  26. };
  27.  
  28. class TaskType1 : public TaskBase<TaskType1> {
  29. public:
  30. void DoImpl() {
  31. cout << "TaskType1::DoImpl()" << endl;
  32. }
  33. };
  34.  
  35. class TaskType2 : public TaskBase<TaskType2> {
  36. public:
  37. void DoImpl() {
  38. cout << "TaskType2::DoImpl()" << endl;
  39. }
  40. };
  41.  
  42. int main() {
  43. std::vector<TaskInterface*> tasks;
  44. TaskType1 t1;
  45. TaskType2 t2;
  46. tasks.push_back(&t1);
  47. tasks.push_back(&t2);
  48.  
  49. for(std::vector<TaskInterface*>::iterator it = tasks.begin();
  50. it != tasks.end();
  51. ++it) {
  52. (*it)->Do();
  53. }
  54. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
TaskType1::DoImpl()
TaskType2::DoImpl()