C++ question: feature similar to Obj-C protocols?

前端 未结 1 895
后悔当初
后悔当初 2021-02-07 19:09

I\'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 exampl

相关标签:
1条回答
  • 2021-02-07 19:45

    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) { }
    };
    
    0 讨论(0)
提交回复
热议问题