波奇C++11:智能指针(三)特殊类的设计和单例模式

智能指针除了创建普通对象还有以下用法

std::shared_ptr<Test> sp1(new Test[2]); // 数组
std::shared_ptr<Test> sp2((Test*)malloc(sizeof(Test))); //malloc开辟空间
std::shared_ptr<FILE> sp3(fopen("源.cpp", "r")); // 文件管理

此时原本的析构的函数可能无法发挥作用。 

~SharedPtr()
	{
		if (--(*_pcount) == 0)
		{
			
			cout << "delete pointer" << _ptr << endl;
			delete _ptr;
			delete _pcount;
		}
		else
		{
			cout << *_pcount << endl;
		}
	}

C++支持shared_ptr 定制删除器,即手动传递构造函数。

template<class T>
struct DeleteArray
{
	void operator()(T* ptr)
	{
		delete[] ptr;
	}
};
std::shared_ptr<Test> sp1(new Test[2],DeleteArray<Test>());
std::shared_ptr<Test> sp2((Test*)malloc(sizeof(Test)), [](Test* sp) {free(sp); });
std::shared_ptr<FILE> sp3(fopen("源.cpp", "r"), [](FILE* sp) {fclose(sp); });

 用function<void(T*)> 来接收可调用对象类型。

template<class T>
class SharedPtr
{
public:
    //添加模板类型,接收可调用对象
	template<class D>
	SharedPtr(T* ptr,D del)
		:_ptr(ptr)
		,_pcount(new int(1))
		,_del(del)
	{}
	~SharedPtr()
	{
		if (--(*_pcount) == 0)
		{
			
			cout << "delete pointer" << _ptr << endl;
			_del(_ptr); //调用
				delete _pcount;
		}
		else
		{
			cout << *_pcount << endl;
		}
	}
//.......
private:

	T* _ptr;
	int* _pcount;
    // 不能直接用D定义类型,用function来接收可调用对象
	function<void(T*)> _del=[](T* ptr){delete ptr;};
};

 特殊类的设计

不能被拷贝的类

class CopyBan
{
    // ...
    CopyBan(const CopyBan&)=delete;
    CopyBan& operator=(const CopyBan&)=delete;
    //...
};

只能在堆上创建

class HeapOnly    
{     
public:     
    static HeapOnly* CreateObject()  
   {      
        return new HeapOnly;    
   }
private:    
    HeapOnly() {}
    // C++11   防止 HeapOnly hp(*hptr);
    HeapOnly(const HeapOnly& hp) = delete;
    HeapOnly& operator()(const HeapOnly& hp)=delete;
};

只能在栈上创建的对象

class StackOnly
{
public:
 static StackOnly CreateObj()
 {
     return StackOnly();
 }
    
 // 禁掉operator new可以把下面用new 调用拷贝构造申请对象给禁掉
 // StackOnly obj = StackOnly::CreateObj();
 // StackOnly* ptr3 = new StackOnly(obj); //调用拷贝构造
 void* operator new(size_t size) = delete;
 void operator delete(void* p) = delete;
private:
 StackOnly()  
 :_a(0)
 {}
 int _a;
};

不能被继承的类

class A  final
{
    // ....
};

单例模式:全局只能创建一个对象

1.构造函数私有化

2.防止拷贝

3.提供获取单例的接口和其他函数 

饿汉模式 

class Singleton
{
public:
	//获取单例对象的接口
	static Singleton& GetInstance()
	{
		return _sinst;
	}
	void test()
	{
		cout << 1;
	}
private:
	// 构造函数私有
	Singleton(){}
	// 防止拷贝
	Singleton(const Singleton& s) = delete;
	Singleton& operator()(const Singleton& s) = delete;
	map<string, string> _dict;
	static Singleton _sinst; //唯一的单例定义在类部
};
Singleton Singleton::_sinst;
int main()
{
    // 调用
	Singleton::GetInstance().test();
	return 0;
}

上面饿汉模式:在main函数之前创建单例对象

存在问题:单例对象初始化内容多,影响启动速度。两个单例类有依赖关系

 懒汉模式:在main函数后创建对象

class Singleton
{
public:
	//获取单例对象的接口
	static Singleton& GetInstance()
	{
        //第一次调用时才创建对象
		if(!_psinst)
        {
            _psinst=new Singleton;
        }
        return *_psinst;
	}
    
	void test()
	{
		cout << 1;
	}
private:
	// 构造函数私有
	Singleton(){}
	// 防止拷贝
	Singleton(const Singleton& s) = delete;
	Singleton& operator()(const Singleton& s) = delete;
	map<string, string> _dict;
	static Singleton* _psinst; //唯一的单例定义在类部
};
Singleton* Singleton::_psinst=nullptr;

单例显性释放

static void DelInstance()
    {
        if(_psinst)
        {
            delete _psinst;
            _psinst=nullptr;
        }
    }

 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值