C++ 单例类

单例类

template <typename T>
template <typename T>
class singleton
{
  public:
    static T& example()
    {
        static T example;
        return example;
    }

    singleton(T&&) = delete;
    singleton(const T&) = delete;
    void operator=(const T&) = delete;

  protected:
    singleton() = default;
    virtual ~singleton() = default;
};

继承这个单例

class entity : public singleton<entity>
{
  public:
    entity();
    ~entity();
		std::string name;
  private:
};

entity::entity()
{
}

entity::~entity()
{
}

类的调用

其它函数中
{
	std::string e_name = entity::example().name;
}

你可能感兴趣的:(C++,c++,开发语言)