fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. template<typename T>
  7. class Base
  8. {
  9. public:
  10. Base(int X) { x = X; }
  11.  
  12. friend T operator+(const Base& lhs, const Base& rhs)
  13. {
  14. return T(lhs.x + rhs.x);
  15. }
  16.  
  17. int x;
  18. };
  19.  
  20. class Derived : public Base<Derived>
  21. {
  22. public:
  23. Derived(int arg) : Base(arg) { }
  24. using Base::Base;
  25. };
  26.  
  27. int main()
  28. {
  29. Derived der1(1);
  30. Derived der2(2);
  31.  
  32. Derived der3 = der1 + der2;
  33.  
  34. cout << der3.x << endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
3