zoukankan      html  css  js  c++  java
  • C++的Singleton模式实现

        Singleton.h

        
    class A {
        
    private:
            
    static A *_instance;
        
    protected:
            A();
        
    public:
            
    static A* getInstance();
            
    void sayhello();
        };

        Singleton.cpp

        #include 
    <iostream>
        #include 
    "singleton.h"
        
    using namespace std;

        A
    * A::_instance = 0;

        A::A() {}
        A
    * A::getInstance() {
            
    if (_instance == 0)
                _instance 
    = new A;
            
    return _instance;
        }
        
    void A::sayhello() {
            cout 
    << "Hello!" << endl;
        }

        testsingleton.cpp

        #include 
    "singleton.h"
        #include 
    <iostream>
        
    using namespace std;

        
    int main()
        {
            A 
    *abc = A::getInstance();
            abc
    ->sayhello();
            
    return 0;
        }
  • 相关阅读:
    GridView使用技巧
    ilspy反编译
    Editplus php
    SQL 日期相减(间隔)datediff函数
    cmd创建文件命令
    iis7 bug解决
    删除qq互联
    discuz 数据库文件密码修改
    linux zip命令
    asp.net调用js方法
  • 原文地址:https://www.cnblogs.com/super119/p/2005624.html
Copyright © 2011-2022 走看看