Is it possible to forbid deriving from a class at compile time?

后端 未结 7 2100
甜味超标
甜味超标 2021-01-02 04:29

I have a value class according to the description in \"C++ Coding Standards\", Item 32. In short, that means it provides value semantics and does not have any virtual method

7条回答
  •  情歌与酒
    2021-01-02 04:58

    If you are willing to only allow the class to be created by a factory method you can have a private constructor.

    class underivable {
        underivable() { }
        underivable(const underivable&); // not implemented
        underivable& operator=(const underivable&); // not implemented
    public:
        static underivable create() { return underivable(); }
    };
    

提交回复
热议问题