lesson7: 特殊类设计

1. 一个类: 只能在堆上创建对象 

关键点:自己控制析构

1.1 方法一: 使用delete禁掉默认析构函数 

#include <iostream>
using namespace std;


class HeapOnly
{
public:
	HeapOnly()
	{
		_str = new char[10];
	}

	~HeapOnly() = delete;

	void Destroy()
	{
		delete[] _str;

		operator delete(this);
	}

private:
	char* _str;
	//...
};

int main()
{
	HeapOnly* ptr = new HeapOnly;
	ptr->Destroy();
	return 0;
}
  • 只要在堆上申请空间,并且使用delete析构函数禁掉就行了 
  • 自己再实现一个释放空间的函数

1.2 方法二: 将析构函数私有化 

#include <iostream>
#include <stdlib.h>
using namespace std;
class HeapOnly
{
public:
	/*static void Delete(HeapOnly* p)
	{
		delete p;
	}*/
	void Delete()
	{
		delete this;
	}

private:
	// 析构函数私有
	~HeapOnly()
	{
		cout << "~HeapOnly()" << endl;
	}
private:
	int _a;
};

int main()
{
	//HeapOnly hp1;// error
	//static HeapOnly hp2;// error

	HeapOnly* ptr = new HeapOnly;
	ptr->Delete();
	return 0;
}

  1.3 方法三: 将构造函数私有化(禁掉拷贝)

#include <iostream>
#include <stdlib.h>
using namespace std;
class HeapOnly
{
public:
	// 提供一个公有的,获取对象的方式,对象控制是new出来的
	static HeapOnly* CreateObj()
	{
		return new HeapOnly;
	}

	// 防拷贝
	HeapOnly(const HeapOnly& hp) = delete;
	HeapOnly& operator=(const HeapOnly& hp) = delete;
private:
	// 构造函数私有
	HeapOnly()
		:_a(0)
	{}
private:
	int _a;
};

int main()
{
	/*HeapOnly hp1;
	static HeapOnly hp2;

	HeapOnly* hp3 = new HeapOnly;
	delete hp3;*/

	HeapOnly* hp3 = HeapOnly::CreateObj();
	//HeapOnly copy(*hp3);

	delete hp3;

	return 0;
}

直接将构造函数私有化,然后再实现一个CreatObj创建对象,返回值是static;

创建的是堆的话,需要禁掉那2个函数


2. 一个类: 只能在栈上创建对象

关键点: 自己控制构造 

2.1 方法一: 构造函数私有化(禁掉new) 

#include <iostream>
#include <stdlib.h>
using namespace std;
class StackOnly
{
public:
	static StackOnly CreateObj()
	{
		StackOnly st;
		return st;
	}

	// 不能防拷贝
	//StackOnly(const StackOnly& st) = delete;
	//StackOnly& operator=(const StackOnly& st) = delete;
	void* operator new(size_t n) = delete;
private:
	// 构造函数私有
	StackOnly()
		:_a(0)
	{}
private:
	int _a;
};

int main()
{
	/*StackOnly st1;
	static StackOnly st2;
	StackOnly* st3 = new StackOnly;*/

	StackOnly st1 = StackOnly::CreateObj();

	// 拷贝构造
	static StackOnly copy2(st1); // 不好处理,算是一个小缺陷
	//StackOnly* copy3 = new StackOnly(st1);

	return 0;
}

 3. 一个类:不能被继承

3.1 给父类加final关键字 

#include <iostream>
#include <string>
using namespace std;

//C98
//class A
//{
//private:
//	A()
//	{}
//
//protected:
//	int _a;
//};

// C++11中引用的final
class A final
{
public:
	A()
	{}

protected:
	int _a;
};

class B : public A
{

};

int main()
{
	B bb;// 这里对象实例化才会报错

	return 0;
}
  •  C++98中:a. 父类构造函数私有-- 子类是不可见,b. 这种只有对象实例化才会报错

  • C++11中:给父类加上了final关键字,使子类不能继承父类,

4. 一个类: 只能创建一个对象(单例模式)

4.1 单例模式(饿汉模式 && 懒汉模式)

那两种模式都是将构造函数私有化,自己实现一个构造生成一个静态对象

  • 一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个 访问它的全局访问点,该实例被所有程序模块共享

4.2 饿汉模式: 程序启动时就创建一个唯一的实例对象

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

	// C++11 : 防拷贝
	Singleton(Singleton const&) = delete;
	Singleton& operator=(Singleton const&) = delete;

	static Singleton m_instance;// 声明
};

Singleton Singleton::m_instance;// 定义
  • 优点:简单
  • 缺点:可能会导致进程启动慢,且如果 有多个单例类对象实例启动顺序 不确定。
  • 总结: 如果这个单例对象在多线程高并发环境下频繁使用,性能要求较高,那么显然使用饿汉模式来避 免资源竞争,提高响应速度更好。

4.3 懒汉模式 : 第一次使用对象再创建实例对象

  • 如果单例对象构造十分耗时或者占用很多资源,比如加载插件啊, 初始化网络连接啊,读取 文件啊等等,而有可能该对象程序运行时不会用到,那么也要在程序一开始就进行初始化,
  • 就会导致程序启动时非常的缓慢。 所以这种情况使用懒汉模式(延迟加载)更好。
#include <iostream>
#include <stdlib.h>
using namespace std;
class MemoryPool
{
public:
	static MemoryPool* GetInstance()
	{
		if (_pinst == nullptr) {
			_pinst = new MemoryPool;
		}

		return _pinst;
	}

	void* Alloc(size_t n)
	{
		void* ptr = nullptr;
		// ....
		return ptr;
	}

	void Dealloc(void* ptr)
	{
		// ...
	}

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

private:
	// 构造函数私有化
	MemoryPool()
	{
		// ....
	}

	char* _ptr = nullptr;
	// ...

	static MemoryPool* _pinst; // 声明
};

// 定义
MemoryPool* MemoryPool::_pinst = nullptr;

// 回收对象,main函数结束后,他会调用析构函数,就会释放单例对象
static MemoryPool::CGarbo gc;

int main()
{
	void* ptr1 = MemoryPool::GetInstance()->Alloc(10);
	MemoryPool::GetInstance()->Dealloc(ptr1);
}
  • 优点: 有控制顺序, 不影响启动速度
  • 缺点: 相对复杂, 存在线程安全问题

4.4 单例对象释放问题:

  1. 一般情况下,单例对象不需要释放的。因为一般整个程序运行期间都可能会用它。单例对象在进程正常结束后,也会资源释放。
  2. 有些特殊场景需要释放,比如单例对象析构时,要进行一些持久化(往文件、数据库写)操作。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值