fork(1) download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class Base
  5. {
  6. public:
  7. void method() {
  8. static_cast<T*>(this)->method();
  9. }
  10. };
  11.  
  12. class Derived1 : public Base<Derived1>
  13. {
  14. public:
  15. void method() {
  16. std::cout << "Derived1 method" << std::endl;
  17. }
  18. };
  19.  
  20.  
  21. class Derived2 : public Base<Derived2>
  22. {
  23. public:
  24. void method() {
  25. std::cout << "Derived2 method" << std::endl;
  26. }
  27. };
  28.  
  29.  
  30. int main()
  31. {
  32. Base<Derived1> *d1 = new Derived1;
  33. Base<Derived2> *d2 = new Derived2;
  34. d1->method();
  35. d2->method();
  36. return 0;
  37. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
Derived1 method
Derived2 method