C++ question: feature similar to Obj-C protocols?
I'开发者_JS百科m used to using Objective-C protocols in my code; they're incredible for a lot of things. However, in C++ I'm not sure how to accomplish the same thing. Here's an example:
- Table view, which has a function setDelegate(Protocol *delegate)
- Delegate of class Class, but implementing the protocol 'Protocol'
- Delegate of class Class2, also implementing 'Protocol'
- setDelegate(objOfClass) and setDelegate(objOfClass2) are both valid
In Obj-C this is simple enough, but I can't figure out how to do it in C++. Is it even possible?
Basically, instead of "Protocol" think "base class with pure virtual functions", sometimes called an interface in other languages.
class Protocol
{
public:
virtual void Foo() = 0;
};
class Class : public Protocol
{
public:
void Foo() { }
};
class Class2 : public Protocol
{
public:
void Foo() { }
};
class TableView
{
public:
void setDelegate(Protocol* proto) { }
};
精彩评论