最简单的单例模式

//单例模式
#include <iostream>
using namespace std;

class Singleton
{
public:
    static Singleton* Instance();
    
protected:
    Singleton() {}
    
private:
    static Singleton* _instance;
};
Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance()
{
    if (_instance == 0)
    {
        _instance = new Singleton();
        cout << "Singleton new" << endl;
    }
    return _instance;
}

int main()
{
    for (int i=0; i<10; i++)
    {
        Singleton* obj = Singleton::Instance();
    }
    return 0;
}
image
posted @ 2012-10-11 20:28  涵曦  阅读(793)  评论(0编辑  收藏  举报