[2023.4.9]C++单例模式(线程安全)

https://blog.csdn.net/yockie/article/details/40790347
https://blog.csdn.net/u010029439/article/details/79439332
class Singleton   
{   
private:   
    Singleton();
    ~Singleton();
public:   
    static Singleton &instance()   
    {   
        Lock(); // not needed after C++0x   
        static Singleton instance;   
        UnLock(); // not needed after C++0x   
        return instance;    
    }   
};
C++11,先前被称作C++0x
instance对象位于全局区,new的话是堆区,不用static的话是栈区。
模板化:
template
class Singleton {
public:
    static T& GetInstance()
    {
        static T instance;
        return instance;
    }
private:
    // 防止函数被调用
    Singleton() = delete;
    ~Singleton() = delete;
};

你可能感兴趣的:(#,1.3.1,C++经验,c++)