Making a class abstract without any pure virtual methods

前端 未结 5 1533
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 19:27

I have a class which is to listen to mouse events. However, I do not want to force the user to implement any specific one, but I do want to make it clear that they must inhe

5条回答
  •  旧时难觅i
    2020-12-23 20:17

    You can declare a pure virtual destructor, but give it a definition. The class will be abstract, but any inheriting classes will not by default be abstract.

    struct Abstract
    {
         virtual ~Abstract() = 0;
    };
    
    Abstract::~Abstract() {}
    
    struct Valid: public Abstract
    {
            // Notice you don't need to actually overide the base
            // classes pure virtual method as it has a default
    };
    
    
    int main()
    {
        // Abstract        a;  // This line fails to compile as Abstract is abstract
        Valid           v;  // This compiles fine.
    }
    

提交回复
热议问题