惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
Y
Y Combinator Blog
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
MyScale Blog
MyScale Blog
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
量子位
I
Intezer
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
L
LINUX DO - 最新话题
The Register - Security
The Register - Security
T
Tailwind CSS Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
S
Secure Thoughts
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
L
LangChain Blog
Recorded Future
Recorded Future
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
人人都是产品经理
人人都是产品经理
F
Full Disclosure
O
OpenAI News
Webroot Blog
Webroot Blog
A
Arctic Wolf
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
H
Heimdal Security Blog
B
Blog RSS Feed
Vercel News
Vercel News

博客园 - xcywt

IT人备考心得分享 一个大龄程序员的回乡记 一种刚接触的语法。只在linux可用 固件打包流程 malloc底层实现以及和new的比较 C++异步调用 future async promise packaged_task 记录一道面试题(哈希表 稀疏矩阵) Qt实现自定义控件-按钮 一个线程池的例子 C++ 条件变量condition_variable的例子 C++中share_ptr中循环引用的问题 C++14的一些新特性 C++11的一些特性 ubuntu编译grpc & protobuf perf笔记 一个cmakelist的例子(自动处理多个proto) Linux下eCal测试计划及进度记录 windows编译ecal 记录一次重装gitlab
如果在单例模式中返回share_ptr ???
xcywt · 2024-10-12 · via 博客园 - xcywt

背景:

接触到一个很有意思的题目:如果在单例模式中返回share_ptr ???

static std::shared_ptr<Singleton> getInstance() ;

分析:

这个问题的难点在于如果要实现单例,那么一定要把构造函数变成私有的,但是make_shared一定是调用的public的函数。

破题思路是:把构造函数变成public,但是通过别的手段限制外部调用。 同时又保证 getInstance可以调用到。

具体而言:定义一个私有的类成员,在构造函数中通过参数传递进来。

上代码:

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

class Singleton {
private:
    struct privatenum {
        int num;
    }k;
public:
    // 获取单例实例的方法
    static std::shared_ptr<Singleton> getInstance() {
        // 使用双重检查锁定来确保线程安全
        static std::shared_ptr<Singleton> instance; // 未初始化
        if (!instance) {
            std::lock_guard<std::mutex> lock(mutex_); // 锁定以确保线程安全
            if (!instance) {
                privatenum k;
                instance = std::make_shared<Singleton>(k); // 使用make_shared创建实例
            }
        }
        return instance;
    }

    // 其他成员函数
    void showMessage() {
        std::cout << "Hello from Singleton!" << std::endl;
    }

  // 只要 k是私有的,那么就不能在外面调用。 Singleton(privatenum k) {};
private: // 私有化构造函数,避免外部实例化 // 禁用拷贝构造函数和赋值操作符 Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; static std::mutex mutex_; // 互斥锁 }; // 定义静态成员变量 std::mutex Singleton::mutex_; int main() { // 使用单例 std::shared_ptr<Singleton> singleton1 = Singleton::getInstance(); singleton1->showMessage(); std::shared_ptr<Singleton> singleton2 = Singleton::getInstance(); singleton2->showMessage(); // 检查两个实例是否相同 std::cout << "Instances are equal: " << (singleton1 == singleton2) << std::endl; return 0; }