fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class A
  6. {
  7. public:
  8. virtual void call_bar() = 0;
  9. virtual ~A() {}
  10. };
  11.  
  12. template <typename T>
  13. class B : public A
  14. {
  15. public:
  16. virtual void call_bar() override
  17. {
  18. bar(static_cast<T*>(this));
  19. }
  20. };
  21.  
  22. class C: public B<C>
  23. {
  24. };
  25.  
  26. void bar(C* c)
  27. {
  28. cout << "C" << endl;
  29. }
  30.  
  31. typedef std::vector<A*> a_list;
  32.  
  33. int main() {
  34. C c;
  35. A* a = &c;
  36. a->call_bar();
  37.  
  38. a_list b;
  39. b.push_back(a);
  40. for(auto& ref : b)
  41. ref->call_bar();
  42. // your code goes here
  43. return 0;
  44. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
C
C