fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <typename C>
  5. class Super
  6. {
  7. public:
  8. std::string GetFunnyName() const
  9. {
  10. C *thiz = static_cast<C *>(this);
  11. return thiz->GetFunnyName();
  12. }
  13. };
  14.  
  15. template <typename C>
  16. class A_base : public Super<C>
  17. {
  18. public:
  19. std::string GetFunnyName() const
  20. {
  21. return "A";
  22. }
  23. };
  24. class A : public A_base<A> { };
  25.  
  26. class B : public A_base<B>
  27. {
  28. public:
  29. std::string GetFunnyName() const
  30. {
  31. return "B";
  32. }
  33. };
  34.  
  35. template <typename TSuper>
  36. void OutputFunny(const TSuper &obj)
  37. {
  38. std::cout << obj.GetFunnyName() << "\n";
  39. }
  40.  
  41. int main()
  42. {
  43. A a;
  44. B b;
  45.  
  46. OutputFunny(a);
  47. OutputFunny(b);
  48. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
A
B