最简单的单例模式

//单例模式
#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
原文地址:https://www.cnblogs.com/hanxi/p/2720451.html