class Interface
{
public:
virtual void someMethod() = 0;
virtual void someOtherMethod() const = 0;
};
template<class I>
class ProxyBase
{
public:
template<class> friend class ProxyBase;
ProxyBase(I& actualObject)
: actualObject(&actualObject){}
//conversion ctor
ProxyBase(ProxyBase<Interface> const& other):actualObject(other.actualObject){}
//conversion op=
ProxyBase & operator=(ProxyBase<Interface> const& other){
actualObject = other.actualObject;
return *this;
}
void someMethod()
{
// mache irgendwas
actualObject->someMethod();
}
void someOtherMethod() const
{
// mache irgendwas
actualObject->someOtherMethod();
}
private:
I* actualObject;
};
typedef ProxyBase<Interface> Proxy;
typedef ProxyBase<Interface const> ConstProxy;
#include <iostream>
class ActualObject : public Interface
{
void someMethod()
{
std::cout << "ActualObject::someMethod()\n";
}
void someOtherMethod() const
{
std::cout << "ActualObject::someOtherMethod() const\n";
}
};
int main()
{
const ActualObject someObject;
ActualObject someNonConstObject;
ConstProxy proxy(someObject);
proxy.someOtherMethod();
Proxy proxy2(someNonConstObject);
proxy2.someOtherMethod();
proxy2.someMethod();
}