Making a class abstract without any pure virtual methods

前端 未结 5 1525
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  时光取名叫无心
    2020-12-23 20:04

    Specify the constructor of the base as protected. This does mean that you cannot construct it directly, but forces inheritance. There is nothing that makes a developer inherit from that class aside from good documentation though!

    Example:

    struct Abstract {
    protected:
        Abstract() {}
    };
    
    struct Valid: public Abstract {
        // No need to override anything.
    };
    
    
    int main() {
        // Abstract a;  // This line fails constructor is protected
        Valid v;  // This compiles fine.
    }
    

提交回复
热议问题