How do you declare an interface in C++?

前端 未结 15 2617
借酒劲吻你
借酒劲吻你 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

    You can also consider contract classes implemented with the NVI (Non Virtual Interface Pattern). For instance:

    struct Contract1 : boost::noncopyable
    {
        virtual ~Contract1();
        void f(Parameters p) {
            assert(checkFPreconditions(p)&&"Contract1::f, pre-condition failure");
            // + class invariants.
            do_f(p);
            // Check post-conditions + class invariants.
        }
    private:
        virtual void do_f(Parameters p) = 0;
    };
    ...
    class Concrete : public Contract1, public Contract2
    {
    private:
        virtual void do_f(Parameters p); // From contract 1.
        virtual void do_g(Parameters p); // From contract 2.
    };
    

提交回复
热议问题