//单例模式
#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;
}
最简单的单例模式

相关文章:

  • 2022-03-04
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-26
  • 2021-05-20
  • 2021-12-02
  • 2021-11-24
猜你喜欢
  • 2022-01-23
  • 2021-09-28
  • 2022-12-23
  • 2021-06-03
  • 2021-06-11
相关资源
相似解决方案