C++-特殊类设计-单例模式

本文介绍了如何在C++中实现特殊类的设计,包括限制对象只能在堆或栈上创建,禁止拷贝与继承,以及实现线程安全的单例模式。详细讲解了不同方法的实现原理,如使用私有构造函数和静态成员函数,以及C++11的`=delete`特性。同时,对比了饥饿模式和懒汉模式的单例实现,强调了线程安全的重要性。
摘要由CSDN通过智能技术生成

特殊类设计

1. 设计一个类,只能在堆上创建对象

class HeapOnly
{
public:
	static HeapOnly* CreateOject()
	{
		return new HeapOnly;
	}

private:
	HeapOnly(){}

	//1.方法一
	HeapOnly(const HeapOnly&);

	//2.方法二
	//C++11
	HeapOnly(const HeapOnly&) = delete;
};

2. 设计一个类,只能在栈上创建对象

方法1

class StackOnly
{
public:
	//将构造函数私有化,然后设计静态方法创建对象返回
	static StackOnly CreateObject()
	{
		return StackOnly();
	}
private:
	StackOnly(){}
};

方法2

class StackOnly
{
public:
	StackOnly(){}
private:
	void* operator new(size_t size);
	void operator delete(void* p);
};

3. 设计一个类,不能被拷贝

方法1

class CopyBan
{
	//拷贝存在于 1.拷贝构造函数 2.赋值运算符
	//禁止拷贝只需要把这两个函数放到private,只声明不定义即可
private:
	CopyBan(const CopyBan&);
	CopyBan& operator=(const CopyBan&);
};

方法2

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

4. 设计一个类,不能被继承

方法1

class NonInherit
{
public:
	static NonInherit GetInstance()
	{
		return NonInherit();
	}
private:
	NonInherit()
	{}
};

方法2

class A final
{

};

5. 设计一个类,只能创建一个对象(单例模式)

  使用设计模式主要是为了代码可重用性

5.1 饥饿模式

class Singleton
{
public:
	static Singleton* GetInstance()
	{
		return &m_instance;
	}
private:
	//构造函数
	Singleton(){};

	//c++98 预防拷贝
	Singleton(Singleton const&);
	Singleton& operator=(Singleton const&);

	//C++11
	Singleton(Singleton const&) = delete;
	Singleton& operator=(Singleton const&) = delete;

	static Singleton m_instance;
};

Singleton Singleton::m_instance;

5.2 懒汉模式

//懒汉模式
//优点:第一次使用实例对象时,创建对象,进程启动无负载,多个单例实例启动顺序自由控制
//缺点:复杂
#include <iostream>
#include <mutex>//互斥锁
#include <thread>//线程
using namespace std;

class Singleton
{
public:
	static Singleton* GetInstance()
	{
		if (nullptr == m_pInstance)
		{
			m_mtx.lock();
			if (nullptr == m_pInstance)
			{
				m_pInstance = new Singleton();
			}
			m_mtx.unlock();
		}
		return m_pInstance;
	}

	//实现一个内嵌垃圾回收类
	class CGarbo
	{
	public:
		~CGarbo()
		{
			if (Singleton::m_pInstance)
				delete Singleton::m_pInstance;
		}
	};

	static CGarbo Garbo;

private:
	//构造函数私有
	Singleton(){};

	//防拷贝
	Singleton(Singleton const&);
	Singleton& operator=(Singleton const&);

	//单例对象指针
	static Singleton* m_pInstance;
	//互斥锁
	static mutex m_mtx;
};

Singleton* Singleton::m_pInstance = nullptr;
Singleton::CGarbo Garbo;
mutex Singleton::m_mtx;

void func(int n)
{
	cout << Singleton::GetInstance() << endl;
}

int main()
{
	thread t1(func, 10);
	thread t2(func, 10);

	t1.join();
	t2.join();

	cout << Singleton::GetInstance() << endl;
	cout << Singleton::GetInstance() << endl;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天津 唐秙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值