【C++进阶】--特殊类设计

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

  • C++98
    将拷贝构造函数与赋值重载只声明不定义,并且将其访问权限设置为私有即可
class CopyBan
{
// ...
private:
	CopyBan(const CopyBan&);
	CopyBan& operator=(const CopyBan&);
//...
};

  • C++11
class CopyBan
{
// ...
   CopyBan(const CopyBan&)=delete;
   CopyBan& operator=(const CopyBan&)=delete;
//...
};

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

思路:
1、将类的构造函数设置为private,拷贝构造声明为private。防止别人调用拷贝在栈上生成对象
2、提供一个静态的成员函数,在该静态成员函数中玩成对堆对象的创建

#include<iostream>
using namespace std;
class HeapOnly
{
public:
   void Destroy()
   {
   	delete this;
   }
private:
   ~HeapOnly()
   {
   	cout << "~HeapOnly()" << endl;
   }
   int _x;
};
int main()
{
   //HeapOnly ho1;
   //static HeapOnly ho2;
   HeapOnly* pho3 = new HeapOnly;
   pho3->Destroy();
   return 0;
}

#include<iostream>
using namespace std;
class HeapOnly
{
public:
	static HeapOnly* CreateObj(int x = 0)
	{
		HeapOnly* p = new HeapOnly(x);
		return p;
	}
private:
	HeapOnly(int x)
		:_x(x)
	{}
	HeapOnly(const HeapOnly& hp) = delete;
	HeapOnly&  operator =(const HeapOnly& hp) = delete;

	int _x;
};
int main()
{
	//HeapOnly ho1;
	//static HeapOnly ho2;
	HeapOnly::CreateObj(10);
	//HeapOnly p2(*p1);
	return 0;
}

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

#include<iostream>
using namespace std;
class StackOnly
{
public:
	static StackOnly CreateObj(int x = 0)
	{
		return StackOnly(x);
	}
	//防止拷贝构造被ban之后,无法传值返回
	StackOnly(StackOnly&& st)
	:_x(st._x)
	{
		cout << "StackOnly(StackOnly&& st)" << endl;
	}

private:
	int _x;
	StackOnly(int x)
		:_x(x)
	{
		cout << "StackOnly(int x)" << endl;
	}
	StackOnly(const StackOnly& s) = delete;
};
int main()
{
	StackOnly st1 = StackOnly::CreateObj(1);
	static StackOnly st2 = move(st1);
	//static StackOnly st2 = st1;
	return 0;
}

但是我们虽然ban掉了在堆上创建对象,却无法ban掉静态对象。
运行结果:
在这里插入图片描述

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

  • C++98方法
// C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承
class NonInherit
{
public:
	static NonInherit GetInstance()
	{
		return NonInherit();
	}
private:
	NonInherit()
	{}
};
  • C++11方法
    final修饰类,表示该类不能被继承
class A final
{
// ....
};

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

单例模式:一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享。比如在某个服务器中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息,这种方式简化了在复杂环境下的配置管理。

  • 饿汉模式
    不管用不用,程序启动就创建一个唯一的实例对象
    优点:简单
    缺点:可能会导致进程启动慢,且如果有多个单例类对象实例启动顺序不确定
#include<iostream>
#include<vector>
#include<string>
#include<mutex>
#include<thread>
using namespace std;

class Singleton
{
public:
	static Singleton* GetInstance()
	{
		return _ins;
	}
	void Add(const string& str)
	{
		_mtx.lock();
		_v.push_back(str);
		_mtx.unlock();
	}
	void Print()
	{
		_mtx.lock();
		for (auto& e : _v)
		{
			cout << e ;
		}
		cout << endl;
		_mtx.unlock();
	}
private:
	mutex _mtx;
	vector<string> _v;
	static Singleton* _ins;
	Singleton()
	{}
};
Singleton* Singleton::_ins = new Singleton;
int main()
{
	int n = 10;
	thread t1([n]() {
		for (size_t i = 0; i < n; ++i)
		{
			Singleton::GetInstance()->Add("t1线程"+to_string(rand())+'\n');
		}
		});
	thread t2([n]() {
		for (size_t i = 0; i < n; ++i)
		{
			Singleton::GetInstance()->Add("t2线程"+to_string(rand())+'\n');
		}
		});
	t1.join();
	t2.join();


	Singleton::GetInstance()->Print();
	return 0;
}

运行结果:
在这里插入图片描述

  • 懒汉模式

优点:第一次使用实例对象时,创建对象。进程启动无负载。多个单例实例启动顺序自由控制。
缺点:复杂

#include<iostream>
#include<vector>
#include<string>
#include<mutex>
#include<thread>
using namespace std;

class Singleton
{
public:
	static Singleton* GetInstance()
	{
		//双检查加锁
		//提高效率,只有第一次创建实例对象时才加锁
		if (_ins == nullptr)
		{
			_smtx.lock();
			//保证线程安全(防止多线程多次调用new,造成实例对象不唯一)
			if (_ins == nullptr)
			{
				_ins = new Singleton;
			}
			_smtx.unlock();
		}
		return _ins;
	}
	void Add(const string& str)
	{
		_mtx.lock();
		_v.push_back(str);
		_mtx.unlock();
	}
	void Print()
	{
		_mtx.lock();
		for (auto& e : _v)
		{
			cout << e;
		}
		cout << endl;
		_mtx.unlock();
	}
	//一般全局都要使用单例对象,所以单例对象一般不需要释放
	static void DelInstance()
	{
		_smtx.lock();
		if (_ins)
		{
			delete _ins;
			_ins = nullptr;
		}
		_smtx.unlock();
	}
	//单例对象回收
	class GC
	{
	public:
		~GC()
		{
			DelInstance();
		}
	};
	static GC _gc;
private:
	mutex _mtx;
	static mutex _smtx;
	vector<string> _v;
	static Singleton* _ins;

	Singleton()
	{}
};
Singleton* Singleton::_ins = nullptr;
mutex Singleton::_smtx;
Singleton::GC Singleton::_gc;

int main()
{
	int n = 10;
	thread t1([n]() {
		for (size_t i = 0; i < n; ++i)
		{
			Singleton::GetInstance()->Add("t1线程"+to_string(rand())+'\n');
		}
		});
	thread t2([n]() {
		for (size_t i = 0; i < n; ++i)
		{
			Singleton::GetInstance()->Add("t2线程"+to_string(rand())+'\n');
		}
		});
	t1.join();
	t2.join();

	Singleton::GetInstance()->Print();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mr Maria

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

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

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

打赏作者

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

抵扣说明:

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

余额充值