fork download
  1. #include <cstdio>
  2.  
  3. template <class T>
  4. class Writer
  5. {
  6. public:
  7. Writer() { }
  8. ~Writer() { }
  9.  
  10. void write(const char* str) const
  11. {
  12. static_cast<const T*>(this)->writeImpl(str);
  13. }
  14. };
  15.  
  16. class ConsoleWriter : public Writer<ConsoleWriter>
  17. {
  18. public:
  19. ConsoleWriter() { }
  20. ~ConsoleWriter() { }
  21.  
  22. void writeImpl(const char* str) const
  23. {
  24. printf("%s\n", str);
  25. }
  26. };
  27.  
  28. class ConsoleWriterNoInheritance
  29. {
  30. public:
  31. ConsoleWriterNoInheritance() { }
  32. ~ConsoleWriterNoInheritance() { }
  33.  
  34. void write(const char* str) const
  35. {
  36. printf("%s\n", str);
  37. }
  38. };
  39.  
  40.  
  41. int main()
  42. {
  43. ConsoleWriter writer;
  44. writer.write("Hello world");
  45. ConsoleWriterNoInheritance other_writer;
  46. other_writer.write("Other");
  47. return 0;
  48. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Hello world
Other