zoukankan      html  css  js  c++  java
  • C/C++之单例模式实现

    /***
    * 保证一个类仅有一个实例,并提供一个访问它的全局访问点
    */

    #include <iostream>
    #include <string>
    using namespace std;

    class Singelton
    {
    private:
            Singelton(){}
            static Singelton* singel;

    public:
            static Singelton* GetInstance()
            {
                    if(singel == NULL)
                    {
                            singel = new Singelton();
                    }
                    return singel;
            }

    };
    Singelton* Singelton::singel = NULL;//注意静态变量类外初始化

    //客户端:
    int main()
    {
            Singelton* s1=Singelton::GetInstance();
            Singelton* s2=Singelton::GetInstance();
            if(s1 == s2)
                    cout<<"ok"<<endl;
            else
                    cout<<"no"<<endl;        
          return 0;
    }
  • 相关阅读:
    leetcode
    leetcode
    leetcode
    leetcode
    Postman
    Fiddler抓百度和bing搜索包
    Jmeter脚本录制
    Android Studio使用Maven国内镜像
    Open Live Writer
    PureXXX使用手记
  • 原文地址:https://www.cnblogs.com/ht-927/p/4726560.html
Copyright © 2011-2022 走看看