zoukankan      html  css  js  c++  java
  • 单例

    1、懒汉式

    2、饿汉式

    3、加锁

    4、双重加锁

    5、智能指针

    6、模板

    template <typename T>

    class Singleton

    {

    public:

      static T& getInstance()

      {

        init();

        return instance;

      }

      ~Singleton(){}

    private :

      static void init()

      {

        if(0 == instance)

        {

          instance = new T;

          atexit(destroy);//注册destroy函数,当程序结束时,自动调用destroy函数    

        }  

      }  

      static void destroy()

      {

        delete instance;

      }

      Singleton(){}

      Singleton(const T& other){}

      Singleton& operator=(cosnt T& other){}

      static T* instance;

    }

    template<typename T>

    T* Singleton<T>::instance = 0;

  • 相关阅读:
    PKU1008
    PKU 1007
    PKU 3983
    PKU 1005
    PKU1004
    PKU 1003解题
    new.target
    debugger 关键字
    React 高阶组件
    CSS|规范
  • 原文地址:https://www.cnblogs.com/vinke2013/p/8110357.html
Copyright © 2011-2022 走看看