Can I use `abstract` keyword in C++ class

前端 未结 10 2268
梦如初夏
梦如初夏 2020-12-06 09:10

Can we write abstract keyword in C++ class?

10条回答
  •  盖世英雄少女心
    2020-12-06 10:04

    As others point out, if you add a pure virtual function, the class becomes abstract.

    However, if you want to implement an abstract base class with no pure virtual members, I find it useful to make the constructor protected. This way, you force the user to subclass the ABC to use it.

    Example:

    class Base
    {
    protected:
        Base()
        {
        }
    
    public:
        void foo()
        {
        }
    
        void bar()
        {
        }
    };
    
    class Child : public Base
    {
    public:
        Child()
        {
        }
    };
    

提交回复
热议问题