C++可继承的单例基类模板

本文介绍了一种C++单例模式的实现方法,通过模板基类和CRTP模式,使得任何类只需简单继承即可轻松实现线程安全的单例。代码在GitHub上可供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、介绍

最近在写一个项目,其中用到好几个单例,类本身的设计不是很复杂,但是如果每个都写一遍单例又觉得有点冗余;所以查资料写了一个单例基类模板,只要一个类继承这个基类并且以自身作为模板参数就可以实现一个单例;关于单例本身的介绍在这里不重点介绍,感兴趣的可以看一下我的另外一篇博文(https://www.cnblogs.com/sunchaothu/p/10389842.html),写的非常详细,把各种单例实现方法总结了一遍,并且给出技术细节分析。

特点:

  • 线程安全,使用 staic local,保证线程安全 (C++11)
  • 可以继承的模板,更加方便的实现单例

二、代码

// author: sunchaothu
// brief: a singleton base class offering an easy way to create singleton
#include <iostream>

template<typename T>
class Singleton{
public:
    static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value){
        static T instance;
        return instance;
    }
    virtual ~Singleton() noexcept{
        std::cout<<"destructor called!"<<std::endl;
    }
    Singleton(const Singleton&)=delete;
    Singleton& operator =(const Singleton&)=delete;
protected:
    Singleton(){
        std::cout<<"constructor called!"<<std::endl;
    }

};
/********************************************/
// Example:
// 1.friend class declaration is requiered!
// 2.constructor should be private


class DerivedSingle:public Singleton<DerivedSingle>{
   // !!!! attention!!!
   // needs to be friend in order to
   // access the private constructor/destructor
   friend class Singleton<DerivedSingle>;
public:
   DerivedSingle(const DerivedSingle&)=delete;
   DerivedSingle& operator =(const DerivedSingle&)= delete;
private:
   DerivedSingle()=default;
};

int main(int argc, char* argv[]){
    DerivedSingle& instance1 = DerivedSingle::get_instance();
    DerivedSingle& instance2 = DerivedSingle::get_instance();
    return 0;
}

以上代码放在了 我的github中: https://github.com/sunchaothu/AdvancedCpp/tree/master/singleton

三、关键处

  • 子类需要把自己作为模板参数,如 class DerivedSingle:public Singleton<DerivedSingle>;
    这里用到的是CRTP(Curiously recurring template pattern) 奇异循环模板模式
  • 在子类中需要把基类 Singleton 声明为友元;这样才能访问私有构造函数(在这篇博客中还提供了不需要声明为友元的实现)
  • 这个使用了局部静态变量保证了线程安全,成为 Magic Static。

    四、使用限制

    继承了这个类的代码不可以作为基类再被继承。

五、参考资料

以上代码参考了以下链接;如果有纰漏之处,还请指正,以免误人子弟!

  1. https://stackoverflow.com/questions/34519073/inherit-singleton?r=SearchResults
  2. https://www.cnblogs.com/lidabo/p/3949226.html

转载于:https://www.cnblogs.com/sunchaothu/p/10353507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值