0
点赞
收藏
分享

微信扫一扫

第四章 网络层【计算机网络】

zmhc 2023-07-13 阅读 71

目录

1.不能被拷贝的类

2.只能在堆上创建对象的类

3.只能在栈上创建对象的类

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

5. 单例模式


1.不能被拷贝的类

拷贝只会发生在两个场景中:拷贝构造函数以及赋值运算符重载。

因此想要让一个类禁止拷贝, 只需让该类不能调用拷贝构造函数以及赋值运算符重载即可

不同C++版本中的实现思想如下:

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.只能在堆上创建对象的类

//构造方式:
HeapOnly* ptr = HeapOnly::CreateObj();

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

	//注意,要将拷贝构造和赋值重载给删除,防拷贝  
	//防拷贝: HeapOnly hp4(*hp3);
	//防赋值: HeapOnly* p_hp = new HeapOnly(*hp3);
	HeapOnly(const HeapOnly& hp) = delete;
	HeapOnly& operator=(const HeapOnly& hp) = delete;
};
//构造方式:
HeapOnly* ptr = new HeapOnly;
//析构方式:
HeapOnly::Delete(ptr);
ptr._Delete();
class HeapOnly
{
public:
    //直接公有或静态都行
	static void Delete(HeapOnly* p)
	{
		delete p;
	}
	void _Delete()
	{
		delete this;
	}
private:
    ~HeapOnly()
    {}
};

3.只能在栈上创建对象的类

//只能在栈上创建的类
class StackOnly
{
public:
	static StackOnly CreateObj()
	{
		StackOnly st;
		return st;
	}
	//不能防止拷贝
	//因为栈上开辟要使用传值返回,所以无法很好的限制住拷贝构造。
	/*StackOnly(const StackOnly& st) = delete;
	StackOnly& operator=(const StackOnly& st) = delete;*/

	// 禁掉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()
	{}
};

void test_StackOnly()
{
	StackOnly st1 = StackOnly::CreateObj();

	//仍然能使用拷贝构造函数在栈上创建对象不好处理,算是一个小缺陷
	StackOnly copy2(st1);  
}

因为栈上开辟要使用传值返回,传值返回又需要拷贝构造和赋值运算符重载函数,所以无法很好的限制住拷贝构造,这是这个设计模式的一个小缺陷。
 

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

// C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承
class NonInherit
{
public:
 static NonInherit GetInstance()
 {
 return NonInherit();
 }
private:
 NonInherit()
 {}
};
class NonInherit final
{
    // ....
};

5. 单例模式

单例模式有两种实现模式:

下面写一个模拟创建一个内存池,使用单例饿汉模式,由于MemoryPool对象是全局唯一的,我们无需担心内存泄漏或重复释放内存的问题。此外,由于MemoryPool对象的生命周期与整个应用程序相同,我们不需要担心在程序结束时手动释放内存。以下是示例代码:

class MemoryPool
{
public:
	static MemoryPool* GetInstance()
	{
		return _pinst;
	}
	void* Alloc(size_t n)
	{
		void* ptr=nullptr;
		cout << "申请内存" << endl;
		return ptr;
	}
	void Dealloc(void* ptr)  {cout << "释放内存" << endl;}
private:
	//构造函数私有化  防止随意创建对象
	MemoryPool()
	{}
	char* _ptr = nullptr;

	static MemoryPool* _pinst;   //类中声明
};
//类外定义
MemoryPool* MemoryPool::_pinst = new MemoryPool;

void test_Memory()
{
	//申请内存
	void* ptr1 = (MemoryPool::GetInstance())->Alloc(10);
	//释放内存
	MemoryPool::GetInstance()->Dealloc(ptr1);
}

class _MemoryPool
{
public:
	static _MemoryPool* GetInstance()
	{
		if (_pinst ==nullptr)
		{
			_pinst = new _MemoryPool;
		}
		return _pinst;
	}
	void* Alloc(size_t n)
	{
		void* ptr = nullptr;
		// alloc
		return ptr;
	}

	class CGarbo {
	public:
		~CGarbo()
		{
			if (_pinst) delete _pinst;
		}
	};

private:
	static _MemoryPool* _pinst;
	_MemoryPool()
	{}
	char* _ptr = nullptr;
};


 

举报

相关推荐

0 条评论