C++笔记之单例模式

C++笔记之单例模式

参考笔记:C++笔记之call_once和once_flag
code review

文章目录

  • C++笔记之单例模式
    • 1.返回实例引用
    • 2.返回实例指针
    • 3.单例和智能指针share_ptr结合
    • 4.单例和std::call_once结合
    • 5.单例和std::call_once、unique_ptr结合

1.返回实例引用

在这里插入图片描述

代码

#include <iostream>

class Singleton {
  public:
    static Singleton &getInstance() {
        static Singleton instance;
        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

  private:
    Singleton() {}                          // 禁止外部创建实例
    Singleton(const Singleton &);           // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

int main() {
    Singleton &instance = Singleton::getInstance();
    instance.printMessage();
    return 0;
}

2.返回实例指针

在这里插入图片描述

代码

#include <iostream>

class Singleton {
  public:
    static Singleton *getInstance() {
        if (instance == nullptr) {
            instance = new Singleton();
        }
        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

  private:
    static Singleton *instance;
    Singleton() {}                          // 禁止外部创建实例
    Singleton(const Singleton &);           // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

Singleton *Singleton::instance = nullptr;

int main() {
    Singleton *instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

3.单例和智能指针share_ptr结合

在这里插入图片描述

代码

#include <iostream>
#include <memory>

class Singleton {
  public:
    static std::shared_ptr<Singleton> getInstance() {
        if (!instance) {
            instance = std::shared_ptr<Singleton>(new Singleton());
        }
        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

  private:
    Singleton() {}                          // 禁止外部创建实例
    Singleton(const Singleton &);           // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例

    static std::shared_ptr<Singleton> instance;
};

std::shared_ptr<Singleton> Singleton::instance = nullptr;

int main() {
    std::shared_ptr<Singleton> instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

4.单例和std::call_once结合

在这里插入图片描述

编译运行:
在这里插入图片描述

代码

#include <iostream>
#include <mutex>

class Singleton {
public:
    static Singleton *getInstance() {
        std::call_once(initFlag, [](){
            instance = new Singleton();
        });

        return instance;
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

private:
    static Singleton *instance;
    static std::once_flag initFlag;
    
    Singleton() {} // 禁止外部创建实例
    Singleton(const Singleton &); // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

Singleton *Singleton::instance = nullptr;
std::once_flag Singleton::initFlag;

int main() {
    Singleton *instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

5.单例和std::call_once、unique_ptr结合

在这里插入图片描述

编译运行
在这里插入图片描述

代码

#include <iostream>
#include <mutex>
#include <memory>

class Singleton {
public:
    static Singleton *getInstance() {
        std::call_once(initFlag, [](){
            instance.reset(new Singleton());
        });

        return instance.get();
    }

    void printMessage() {
        std::cout << "hello world!" << std::endl;
    }

private:
    static std::unique_ptr<Singleton> instance;
    static std::once_flag initFlag;

    Singleton() {} // 禁止外部创建实例
    Singleton(const Singleton &); // 禁止复制实例
    Singleton operator=(const Singleton &); // 禁止赋值实例
};

std::unique_ptr<Singleton> Singleton::instance;
std::once_flag Singleton::initFlag;

int main() {
    Singleton *instance = Singleton::getInstance();
    instance->printMessage();
    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/78993.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

数据结构之队列详解(包含例题)

一、队列的概念 队列是一种特殊的线性表&#xff0c;特殊之处在于它只允许在表的前端&#xff08;front&#xff09;进行删除操作&#xff0c;而在表的后端&#xff08;rear&#xff09;进行插入操作&#xff0c;和栈一样&#xff0c;队列是一种操作受限制的线性表。进行插入操…

亿发创新中医药信息化解决方案,自动化煎煮+调剂,打造智能中药房

传统中医药行业逐步复兴&#xff0c;同时互联网科技和人工智能等信息科技助力中医药行业逐步实现数字化转型。利用互联网、物联网、大数据等科技&#xff0c;实现现代科学与传统中医药的结合&#xff0c;提供智能配方颗粒调配系统、中药自动化调剂系统、中药煎配智能管理系统、…

【Docker报错】docker拉取镜像时报错:no such host

报错信息 [rootSoft soft]# docker pull mysql Using default tag: latest Error response from daemon: Head "https://registry-1.docker.io/v2/library/mysql/manifests/latest": dial tcp: lookup registry-1.docker.io on 192.168.80.2:53: no such host解决方法…

Leetcode-每日一题【剑指 Offer 28. 对称的二叉树】

题目 请实现一个函数&#xff0c;用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样&#xff0c;那么它是对称的。 例如&#xff0c;二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称…

Android 网络编程-网络请求

Android 网络编程-网络请求 文章目录 Android 网络编程-网络请求一、主要内容二、开发网络请求前的基本准备1、查看需要请求的网址是否有效&#xff08;1&#xff09;通过网页在线验证&#xff08;2&#xff09;使用专用window网咯请求工具&#xff08;3&#xff09;编写app代码…

多维时序 | MATLAB实现WOA-CNN-BiGRU-Attention多变量时间序列预测

多维时序 | MATLAB实现WOA-CNN-BiGRU-Attention多变量时间序列预测 目录 多维时序 | MATLAB实现WOA-CNN-BiGRU-Attention多变量时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 多维时序 | MATLAB实现WOA-CNN-BiGRU-Attention多变量时间序列预测 1.程…

LVS负载均衡集群-NAT模式部署

集群 集群&#xff1a;将多台主机作为一个整体&#xff0c;然后对外提供相同的服务 集群使用场景&#xff1a;高并发的场景 集群的分类 1.负载均衡器集群 减少响应延迟&#xff0c;提高并发处理的能力 2&#xff0c;高可用集群 增强系统的稳定性可靠性&…

LeetCode 141.环形链表

文章目录 &#x1f4a1;题目分析&#x1f4a1;解题思路&#x1f514;接口源码&#x1f4a1;深度思考❓思考1❓思考2 题目链接&#x1f449; LeetCode 141.环形链表&#x1f448; &#x1f4a1;题目分析 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中…

Echarts地图-全国主要城市空气质量【亲测有效】

参考&#xff1a; Echarts官网实例 效果&#xff1a; 需要通过ajax的方式获取json数据: {"code":100,"msg":"处理成功&#xff01;","extend":{"items":[{"name":"三亚","value":52},{&qu…

适合程序员的DB性能测试工具 JMeter

背景 1、想要一款既要能压数到mysql&#xff0c;又要能压数到postGre&#xff0c;还要能压数到oracle的自动化工具 2、能够很容易编写insert sql&#xff08;因为需要指定表和指定字段类型压数据&#xff09;&#xff0c;然后点击运行按钮后&#xff0c;就能直接运行&#xff…

精彩回顾 | 迪捷软件出席2023ATC汽车电子与软件技术周

2023年8月18日&#xff0c;由ATC汽车技术会议主办&#xff0c;上海市集成电路行业协会支持的“2023ATC汽车电子与软件技术周”在上海市圆满落幕。迪捷软件上海参展之行圆满收官。 ▲开幕式 本次峰会汇聚了整车厂、汽车零部件集团、软硬件方案提供商、软件工具供应商、软件测试…

利用console提高写bug的效率

前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;前端面试题库 自从入坑前端后&#xff0c;日常写bug就没离开过console。 要说用得多&#xff0c;不如说是console.log用得多&#xff0c;console.warn和console.erro…

shell脚本基础

目录 前言 一、概述 &#xff08;一&#xff09;、shell脚本基础概念 &#xff08;二&#xff09;、shell的类型 二、Shell变量 &#xff08;一&#xff09;、组成 1.变量名 2.变量值 &#xff08;二&#xff09;、类型 1.系统内置变量&#xff08;环境变量&#xff09; 2.自定…

Spring-3-Spring AOP概念全面解析

今日目标 能够理解AOP的作用 能够完成AOP的入门案例 能够理解AOP的工作流程 能够说出AOP的五种通知类型 一、AOP 1 AOP简介 思考&#xff1a;什么是AOP,AOP的作用是什么&#xff1f; 1.1 AOP简介和作用【理解】 AOP(Aspect Oriented Programming)面向切面编程&#xff0c;一…

c51单片机串口通信(中断方式接收数据)(单片机--单片机通信)示例代码 附proteus图

单片机一般采用中断方式接受数据&#xff0c;这样便于及时处理 #include "reg51.h" #include "myheader.h" #define uchar unsigned char int szc[10]{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; int bufferc[6]{0}; int sza[6]{0x01,0x02,0x0…

接口测试及接口抓包常用测试工具和方法?

作为测试领域中不可或缺的一环&#xff0c;接口测试和抓包技术在软件开发过程中扮演着至关重要的角色。不论你是新手还是有一些经验的小伙伴&#xff0c;本篇文章都会为你详细介绍接口测试的基本概念、常用测试工具和实际操作技巧&#xff0c;让你轻松掌握这一技能。 接口测试…

MySQL之索引和事务

什么是索引索引怎么用索引的原理事务使用事务事务特性MySQL隔离级别 什么是索引 索引包含数据表所有记录的引用指针&#xff1b;你可以对某一列或者多列创建索引和指定不同的类型&#xff08;唯一索引、主键索引、普通索引等不同类型&#xff1b;他们底层实现也是不同的&#…

【SpringBoot】中的ApplicationRunner接口 和 CommandLineRunner接口

1. ApplicationRunner接口 用法&#xff1a; 类型&#xff1a; 接口 方法&#xff1a; 只定义了一个run方法 使用场景&#xff1a; springBoot项目启动时&#xff0c;若想在启动之后直接执行某一段代码&#xff0c;就可以用 ApplicationRunner这个接口&#xff0c;并实现接口…

线程池中的常见面试题

目录 1. 线程池相比于线程有什么优点 2. 线程池的参数有哪些 3. 线程工厂有什么用 4. 说一下线程的优先级 5. 说一下线程池的执行流程 6. 线程池的拒绝策略有哪些 7. 如何实现自定义拒绝策略 8. 如何判断线程池中的任务是否执行完成 1. 线程池相比于线程有什么优点 有…

Rancher管理K8S

1 介绍 Rancher是一个开源的企业级多集群Kubernetes管理平台&#xff0c;实现了Kubernetes集群在混合云本地数据中心的集中部署与管理&#xff0c;以确保集群的安全性&#xff0c;加速企业数字化转型。Rancher 1.0版本在2016年就已发布&#xff0c;时至今日&#xff0c;Ranche…
最新文章