采用C++实现单例模式

class Singleton  // 这是两种方式
{
private:
    
    static Singleton single;
    Singleton(){}
    Singleton &operator=(Singleton & s)// 屏蔽拷贝构造函数和赋值操作符
    {
    }
    Singleton(const Singleton &s){}
public:
    int m_value;
    static Singleton * getInstance()
    {
        return &single;
    }
};
Singleton Singleton::single;


class CSingleton
{
private:
    CSingleton()   //构造函数是私有的
    {
    }
    CSingleton(const CSingleton &);
    CSingleton & operator = (const CSingleton &);
public:
    static CSingleton & GetInstance()
    {
        static CSingleton instance;   //局部静态变量
        return instance;
    }
};

 

posted on 2013-07-22 15:54  dyc0113  阅读(115)  评论(0编辑  收藏  举报

导航