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;
    }
  • 相关阅读:
    浅谈聚类算法(K-means)
    多步法求解微分方程数值解
    本学期微分方程数值解课程总结(matlab代码)
    Stone Game
    Two Sum IV
    Insert into a Binary Search Tree
    Subtree of Another Tree
    Leaf-Similar Trees
    Diameter of Binary Tree
    Counting Bits
  • 原文地址:https://www.cnblogs.com/ht-927/p/4726560.html
Copyright © 2011-2022 走看看