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

推荐订阅源

S
Secure Thoughts
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
PCI Perspectives
PCI Perspectives
G
GRAHAM CLULEY
H
Hacker News: Front Page
Cloudbric
Cloudbric
Latest news
Latest news
N
News and Events Feed by Topic
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
SecWiki News
SecWiki News
Security Latest
Security Latest
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术
B
Blog RSS Feed
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
Last Week in AI
Last Week in AI
IT之家
IT之家
Jina AI
Jina AI
Y
Y Combinator Blog
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
P
Privacy International News Feed
B
Blog
S
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
Help Net Security
Help Net Security
D
DataBreaches.Net
博客园_首页
G
Google Developers Blog
I
InfoQ
量子位
大猫的无限游戏
大猫的无限游戏
S
Security @ Cisco Blogs

博客园 - 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; }