且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++ 问题:类似于 Obj-C 协议的特性?

更新时间:2022-06-27 01:05:59

基本上,不是协议"而是具有纯虚函数的基类",有时在其他语言中也称为接口.

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) { }
};