逛下bbs,“在C++中实现不可继承的类”,瞒有意思的。

class NoInherite                                                                                                   
{
    friend class Seal;
    private:
        NoInherite(void) {}
        ~NoInherite(void) {}
};

class Seal: virtual private NoInherite
{
    public: 
        Seal(void){}
        ~Seal(void){}
};

注意:NoInherite做为虚基类被Seal继承(virtual不可少,以virtual继承时,若我们尝试去定义了Seal的派生类,由于NoInherite的构造函数需要被Seal的派生类直接调用,而NoInherite的构造函数具有私有访问权限,故编译器会报错)。

相关文章:

  • 2021-05-19
  • 2021-09-30
  • 2021-05-26
  • 2021-07-30
  • 2021-10-27
  • 2021-05-28
  • 2021-12-01
猜你喜欢
  • 2022-02-04
  • 2021-09-28
  • 2021-06-21
  • 2022-12-23
  • 2021-07-09
  • 2021-12-05
  • 2022-12-23
相关资源
相似解决方案