fork download
  1. class Interface
  2. {
  3. public:
  4. virtual void someMethod() = 0;
  5. virtual void someOtherMethod() const = 0;
  6. };
  7.  
  8. template<class I>
  9. class ProxyBase
  10. {
  11. public:
  12. template<class> friend class ProxyBase;
  13.  
  14. ProxyBase(I& actualObject)
  15. : actualObject(&actualObject){}
  16.  
  17. //conversion ctor
  18. ProxyBase(ProxyBase<Interface> const& other):actualObject(other.actualObject){}
  19.  
  20. //conversion op=
  21. ProxyBase & operator=(ProxyBase<Interface> const& other){
  22. actualObject = other.actualObject;
  23. return *this;
  24. }
  25.  
  26. void someMethod()
  27. {
  28. // mache irgendwas
  29. actualObject->someMethod();
  30. }
  31. void someOtherMethod() const
  32. {
  33. // mache irgendwas
  34. actualObject->someOtherMethod();
  35. }
  36.  
  37. private:
  38. I* actualObject;
  39. };
  40.  
  41. typedef ProxyBase<Interface> Proxy;
  42. typedef ProxyBase<Interface const> ConstProxy;
  43.  
  44. #include <iostream>
  45.  
  46. class ActualObject : public Interface
  47. {
  48. void someMethod()
  49. {
  50. std::cout << "ActualObject::someMethod()\n";
  51. }
  52.  
  53. void someOtherMethod() const
  54. {
  55. std::cout << "ActualObject::someOtherMethod() const\n";
  56. }
  57. };
  58.  
  59. int main()
  60. {
  61. const ActualObject someObject;
  62. ActualObject someNonConstObject;
  63.  
  64. ConstProxy proxy(someObject);
  65. proxy.someOtherMethod();
  66.  
  67. Proxy proxy2(someNonConstObject);
  68. proxy2.someOtherMethod();
  69. proxy2.someMethod();
  70. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
ActualObject::someOtherMethod() const
ActualObject::someOtherMethod() const
ActualObject::someMethod()