Simpler “Preventing derived classes” in C++

前端 未结 2 1128
[愿得一人]
[愿得一人] 2021-01-17 23:20

Going under the assumption that there is a legitimate reason for preventing derivation from some class, Bjarne gives a solution here for the answer to \"Can I stop people de

相关标签:
2条回答
  • 2021-01-17 23:53

    Try:

    class D : public B, public virtual final
    {
    };
    
    0 讨论(0)
  • 2021-01-18 00:11

    The solution is rather bad, it could be improved with CRTP (adobe does so) but it won't be a complete solution. The problem with your code is that another programmer that does not want to break your contract (she is a good person) but does not know that she should not derive from your class might want to lock others from deriving from her own class:

    class YourSealedClass : private virtual final {};
    class HerSealedClass : public YourSealedClass, private virtual final {
    };
    

    Note that there is not even malign intention, and the contract is broken. The improvement with CRTP would be:

    template <typename T>
    class seal
    {
    protected:
       seal(){}
       ~seal(){}
    };
    class YourSealedClass : private virtual seal<YourSealedClass> {};
    

    This will catch the previous mistake, as her code would be:

    class HerSealedClass : public YourSealedClass, private virtual seal<HerSealedClass> {};
    

    And as she is not inheriting from seal<YourSealedClass> the compiler will catch it up.

    The problem with this approach is that it does not block some stubborn (or malign) programmer from writing:

    class MalignClass : public YourSealedClass, private virtual seal<YourSealedClass> {};
    

    To become a child of the sealing class, and as such gain access to your own class.

    In C++0x I believe (would have to recheck it) that they will raise the restriction that a template cannot befriend a template argument, and that will be a good step into a generic typesafe solution for a class sealer:

    template <typename T>
    class seal {
       friend class T; // illegal in C++03
       seal() {};
       ~seal() {};
    };
    class MySealedClass : private virtual seal<MySealedClass>
    {};
    

    The advantage of this approach (currently unavailable) is that because the constructor and destructors are private only friends can instantiate the type. Because you are using the CRTP to inherit from an specific instantiation passing your own type as argument, you are telling the compiler that only you can actually instantiate your base, and all pieces fall right into place.

    0 讨论(0)
提交回复
热议问题