zoukankan      html  css  js  c++  java
  • 设计模式 单例模式 使用模板及智能指针

    单例模式

    // Singleton.cpp: 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <iostream>
    #include <memory>


    using namespace std;

    template< typename T >
    class Singleton
    {
    public:
    static std::shared_ptr<T> Instance();
    private:
    Singleton();
    static std::shared_ptr<T> p;
    };


    template<typename T>
    std::shared_ptr<T> Singleton<T>::p = nullptr;

    template<typename T>
    std::shared_ptr<T> Singleton<T>::Instance() {
    if (p == nullptr) {
    p = std::make_shared<T>();
    }

    return p;
    }


    int main()
    {
    std::shared_ptr<int> p = Singleton<int>::Instance();
    std::cout << *p << " ";
    return 0;
    }

    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    首尾相连一维数组的最大子数组和
    二柱子——在线答题
    二维数组求最大值
    最大子数组和 10.11作业
    最大子数组

    异常
    面向对象2
    面向对象1
    java数据类型
  • 原文地址:https://www.cnblogs.com/itdef/p/7461665.html
Copyright © 2011-2022 走看看