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

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


//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、付费专栏及课程。

余额充值