C++不能继承的类

实现一个不能被继承的类


  • 方法1. 将构造函数私化
// C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承
class NonInherit
{
public:
    static NonInherit GetInstance()
    {
    return NonInherit();
    }
private:
    NonInherit()
    {}
};
  • 方法2. 使用C++11关键字final
// C++11给出了新的关键字final禁止继承
class NonInherit final
{};
发布了53 篇原创文章 · 获赞 46 · 访问量 7209

猜你喜欢

转载自blog.csdn.net/new_bee_01/article/details/104329303
今日推荐