fork(1) 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. class A : public Super<A>
  15. {
  16. public:
  17. std::string GetFunnyName() const
  18. {
  19. return "A";
  20. }
  21. };
  22. class B : public Super<B>
  23. {
  24. public:
  25. std::string GetFunnyName() const
  26. {
  27. return "B";
  28. }
  29. };
  30.  
  31. template <typename TSuper>
  32. void OutputFunny(const TSuper &obj)
  33. {
  34. std::cout << obj.GetFunnyName() << "\n";
  35. }
  36.  
  37. int main()
  38. {
  39. A a;
  40. B b;
  41.  
  42. OutputFunny(a);
  43. OutputFunny(b);
  44. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
A
B