一个用C++写的可以继承的单例类

本文介绍了一个基于 C++ 的线程安全单例模式实现案例。该实现使用了 boost 库来确保多线程环境下的安全性,并利用 auto_ptr 管理单例对象的生命周期。文中还提供了一个具体的 MySingleton 类作为示例。

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

之前参考了一篇文章点击打开链接,但在编译的过程中总是无法通过。后来在其中陆续找出一些错误,并做了部分修改,现在终于可以了。如下


//ISingleton.h文件

#ifndef _ISingleton_H_
#define _ISingleton_H_


#include <memory>
#include <boost\thread.hpp>


template <typename T>
class ISingleton
{
public:
    static T* GetInstance(){
    static boost::mutex s_mutex;
    if (s_instance.get() == NULL)
    {
        boost::mutex::scoped_lock lock(s_mutex);
        if (s_instance.get() == NULL)
        {
s_instance.reset(new T());
        }
        // 'lock' will be destructed now. 's_mutex' will be unlocked.
    }
return  s_instance.get();
};


protected:
    ISingleton() { }
    ~ISingleton() { }


    // Use auto_ptr to make sure that the allocated memory for instance
    // will be released when program exits (after main() ends).
    static std::auto_ptr<T> s_instance;


private:
    ISingleton(const ISingleton&);
    ISingleton& operator =(const ISingleton&);
};


template <typename t>
std::auto_ptr<t> ISingleton<t>::s_instance;


#endif


//MySingleton.h文件

#ifndef MySingleton_H
#define MySingleton_H
#include "ISingleton.h"
#include <iostream>


using namespace std;


class MySingleton : public ISingleton<MySingleton>
{
public:


int Count(){
return ++count;
}
private:
int count;
   // blah blah
   MySingleton()
    {
count=0;
        cout << "Construct MySingleton" << endl;
    };


    ~MySingleton()
    {
        cout << "Destruct MySingleton" << endl;
    };
    friend ISingleton<MySingleton>;
    friend class auto_ptr<MySingleton>;


MySingleton(const MySingleton&){};
MySingleton& operator =(const MySingleton&){};
};
#endif


//测试

int _tmain(int argc, _TCHAR* argv[])
{


MySingleton* s;
s=MySingleton::GetInstance();
cout<<s->Count();
int a;
cin>>a;
return 0;
}

模式是一种设计模式,它保证一个类只有一个,并提供全局访问点。在C语言中,可以通过静态变量和函数来实现模式。 下面是一个继承模式的类的示代码: ```c #include <stdio.h> // 基类 typedef struct { int value; } Singleton; // 全局唯一实 static Singleton *instance = NULL; // 获取实的函数 Singleton *get_instance() { if (instance == NULL) { // 第一次调用时创建实 instance = (Singleton *) malloc(sizeof(Singleton)); instance->value = 0; } return instance; } // 继承自基类的子类 typedef struct { Singleton base; // 基类的实作为子类的成员变量 int extra_value; } SubSingleton; // 全局唯一子类实 static SubSingleton *sub_instance = NULL; // 获取子类实的函数 SubSingleton *get_sub_instance() { if (sub_instance == NULL) { // 第一次调用时创建子类实 sub_instance = (SubSingleton *) malloc(sizeof(SubSingleton)); sub_instance->base = *get_instance(); // 基类实的值拷贝给子类实 sub_instance->extra_value = 0; } return sub_instance; } int main() { // 获取基类实并修改值 Singleton *instance1 = get_instance(); instance1->value = 10; printf("instance1 value: %d\n", instance1->value); // 再次获取基类实,应该和上一个是同一个 Singleton *instance2 = get_instance(); printf("instance2 value: %d\n", instance2->value); // 获取子类实并修改值 SubSingleton *sub_instance1 = get_sub_instance(); sub_instance1->extra_value = 5; printf("sub_instance1 value: %d, extra_value: %d\n", sub_instance1->base.value, sub_instance1->extra_value); // 再次获取子类实,应该和上一个是同一个 SubSingleton *sub_instance2 = get_sub_instance(); printf("sub_instance2 value: %d, extra_value: %d\n", sub_instance2->base.value, sub_instance2->extra_value); // 子类实和基类实应该是同一个 printf("sub_instance2 base value: %d\n", sub_instance2->base.value); return 0; } ``` 在上面的代码中,基类Singleton定义了一个整数成员变量value,并通过get_instance函数实现模式。子类SubSingleton继承自基类,并添加了一个额外的整数成员变量extra_value。通过get_sub_instance函数实现子类的模式。 在main函数中,首先获取基类实并修改value的值,然后再次获取基类实,验证它们是同一个。接着获取子类实并修改extra_value的值,然后再次获取子类实,验证它们是同一个,并且子类实和基类实是同一个
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值