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;
        }
  • 相关阅读:
    C语言编程如何实现输出一个回型递增的N阶矩阵(螺旋矩阵)
    cookie测试要点
    一个网页怎么开展测试
    web和app区别
    app功能测试
    复习Linux笔记
    学习python
    微信小程序测试流程
    Redis
    记录
  • 原文地址:https://www.cnblogs.com/super119/p/2005624.html
Copyright © 2011-2022 走看看