C++ 单例模式实现

内容目录
class Singleton {
public:
    static Singleton& getInstance(){
        static Singleton instance;
        return instance;
    }
private:
    Singleton ()= default;
    ~Singleton ()= default;
    Singleton (const Singleton &)= delete;
    Singleton & operator=(const Singleton &)= delete;
};

模板实现

template<typename T>
class Singleton
{
public:
    static T& getInstance()
    {
        static T instance;
        return instance;
    }
private:
    Singleton ()= default;
    ~Singleton ()= default;
    Singleton (const T &)= delete;
    Singleton & operator=(const T &)= delete;
};
打赏作者