Say I have the following abstract class:
class AbstractClass {
public:
AbstractClass() {}
virtual ~AbstractClass() {}
virtual void virtMethod()
{
printf( "%p\n", this );
}
void method()
{
printf( "%p\n", this );
}
...
};
And the following child classes:
class RawClass : public AbstractClass
{
public:
RawClass();
~RawClass();
...
};
class Wrapper : public AbstractClass
{
public:
Wrapper( AbstractClass* wrapee ) : _wrappee ( wrappee ) {}
~Wrapper() {};
void virtMethod()
{
_wrappee->virtMethod();
}
...
private:
AbstractClass _wrappee;
};
Now, say I do the following:
RawClass* r = new RawClass();
Wrapper* w = new Wrapper( r );
w->virtMethod(); // This prints the address of r
w->method(); // This prints the address of w
What I would like to happen is for calls to w->method() to behave the same as calls to w->virtMethod(). However, I can’t do this without making method() virtual and writing an implementation of it in Wrapper which merely calls RawClass::method().
I have clearly got some design issues here, but can’t figure out a way to get this to work, whilst having RawClass and Wrapper prescribe to the same interface.
Any help would be greatly appreciated.
I think you should not derive Wrapper from AbstractClass and instead overload
->operator like a smart pointer so that methods of AbstractClass can be invokedand call it like so
Boost or C++11 already does this for you if you use shared_ptr