How do you declare an interface in C++?

前端 未结 15 2612
借酒劲吻你
借酒劲吻你 2020-11-22 03:26

How do I setup a class that represents an interface? Is this just an abstract base class?

15条回答
  •  别那么骄傲
    2020-11-22 04:06

    If you're using Microsoft's C++ compiler, then you could do the following:

    struct __declspec(novtable) IFoo
    {
        virtual void Bar() = 0;
    };
    
    class Child : public IFoo
    {
    public:
        virtual void Bar() override { /* Do Something */ }
    }
    

    I like this approach because it results in a lot smaller interface code and the generated code size can be significantly smaller. The use of novtable removes all reference to the vtable pointer in that class, so you can never instantiate it directly. See the documentation here - novtable.

提交回复
热议问题