fork download
  1. class Base
  2. {
  3. // Note: You can make this protected, but then you need `friend class Common;` in A, B.
  4. public:
  5. int x, y;
  6. };
  7.  
  8. struct Common
  9. {
  10. // Hide implementation details here, has access data of type T
  11. // (only public stuff can be accessed, unless Common is a friend of T!)
  12. template <class T>
  13. int sum(T *_this) const {
  14. return _this->x + _this->y;
  15. }
  16. };
  17.  
  18. class A : public Base, private Common
  19. {
  20. public:
  21. // Only forward to the actual implementation in Common
  22. int sum() const { return Common::sum(this); }
  23. };
  24.  
  25. class B : public Base, private Common
  26. {
  27. public:
  28. // Only forward to the actual implementation in Common
  29. int sum() const { return Common::sum(this); }
  30. };
  31.  
  32.  
  33. int main() {
  34. return 0;
  35. }
Success #stdin #stdout 0s 2892KB
stdin
Standard input is empty
stdout
Standard output is empty