fork download
  1. #include <iostream>
  2.  
  3. class Interface
  4. {
  5. public:
  6. virtual void someMethod() = 0;
  7. virtual void someOtherMethod() const = 0;
  8. };
  9.  
  10. struct Impl : Interface {
  11. virtual void someMethod() { std::cout << "some method\n"; }
  12. virtual void someOtherMethod() const { std::cout << "some other method\n"; }
  13. };
  14. struct Proxy : Interface{
  15. Proxy(Interface* i) : i(i) {}
  16. Interface* i;
  17. void someMethod() { std::cout << "log "; i->someMethod(); }
  18. void someOtherMethod() const { std::cout << "log "; i->someOtherMethod(); }
  19. };
  20. class ConstProxy {
  21. Proxy p;
  22. public:
  23. ConstProxy(Interface const* i) : p(const_cast<Interface*>(i)) {}
  24. void someOtherMethod() const { p.someOtherMethod(); }
  25. operator Interface const& () { return p; } // imitiert "extends const Interface"
  26. };
  27.  
  28. void foo( Interface& i )
  29. {
  30. i.someOtherMethod();
  31. }
  32. void foo2( Interface const& i )
  33. {
  34. i.someOtherMethod();
  35. }
  36.  
  37. int main()
  38. {
  39. Impl i;
  40. ConstProxy cp(&i);
  41. //foo(cp);
  42. foo2(cp);
  43. }
  44.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
log some other method