C++实现一个线程安全的单例模式

class A
{
public:
    //静态函数,返回引用
    static A &GetInstance()
    {//静态局部变量
        static A s_instance;
        return s_instance;
    }
private:
    //默认构造函数
    A() = default;
    /*
    拷贝构造函数
        用一个已存在对象构造同类型的副本对象时,会调用拷贝构造函数。
        class 类名{
        public:
            类名(const 类名& that){...}
        };
    */
    A(const A &that) = delete;                //禁止使用拷贝构造函数
    A& operator=(const A&that) = delete;    //禁止使用拷贝赋值用算符
};

 

posted @ 2021-03-17 16:13  Truman001  阅读(57)  评论(0编辑  收藏  举报