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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

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