Honouring an abstract class interface without implementing throughoperator->

S

spongejon

I'm trying to write some code which uses a proxy to be able to catch
member functions calls through a pointer.

The following code won't compile due to A being an abstract class and
Proxy doesn't explicitly implement A's interface. However Proxy
behaves like A, if the member function is called through a pointer to
a Proxy object.



Has anyone tried to implement anything like this?

class A
{
public:
virtual void doit() = 0;
};

class B : public A
{
public:
virtual void doit(){}
};

class Proxy : public A
{
public:
Proxy (B *b):_b(b){}

B* operator->()
{
return _b;
}

private:
B* _b;
};

int main(int argc, char *argv[])
{
B b;
Proxy p(&b);
p->doit();
}
 
M

Marcel Müller

Hi!
I'm trying to write some code which uses a proxy to be able to catch
member functions calls through a pointer.
The following code won't compile due to A being an abstract class and
Proxy doesn't explicitly implement A's interface. However Proxy
behaves like A, if the member function is called through a pointer to
a Proxy object.

Proxy do not behave like A, at least not if you use it in the way you
have done in main. Proxy behaves somewhat like a pointer to A. So simply
remove the inheritance of Proxy from A. You don't need it.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top