C++特殊类设计,
目录
编辑
一、不能拷贝的类
二、只能在栈上创建的类,
三、只能在堆上创建的类,
四、不能被继承的类,
五、只有一个对象的类
一、不能拷贝的类
c++98:将拷贝构造和=运算符重载私有化
C++11:delete删除成员函数
//禁止copy的类
//class CopyBan {//c++98
//public:
// CopyBan(int b)
// :a(b)
// {};
//private:
// CopyBan(const CopyBan&) {};
// CopyBan& operator=(const CopyBan) {};
// int a = 1;
//};class CopyBan {//c++11
public:CopyBan(int val):a(val){};CopyBan(const CopyBan&)=delete;CopyBan& operator=(const CopyBan&) =delete;//返回值CopyBan&,连续赋值int a;
};
二、只能在栈上创建的类,
不能在堆上创建的类:new需要调用构造函数所以将构造函数删除或者声明私有化。
class StackOnly{//堆上对象,new调用构造函数,所以私有化构造函数
public:static StackOnly CreateObj() {return StackOnly();}//c++11//void* operator new(size_t size) = delete;//void operator delete(void* ptr) = delete;private://c++98StackOnly() :_a(1) {};//StackOnly(const StackOnly&) {};StackOnly& operator=(const StackOnly&);int _a;
};
三、只能在堆上创建的类,
不能在栈上创建,栈上创建可以调用构造函数或者拷贝构造函数,或者=运算符重载。所以c++98设为私有,c++11:delete
class HeapOnly {//只能在堆上创建对象,栈上的对象,拷贝和构造函数私有化
public:static HeapOnly* CreateObject() {return new HeapOnly;}//c++11//HeapOnly(const HeapOnly&) = delete;
private:HeapOnly():a(1) {}//c++98HeapOnly(const HeapOnly&);HeapOnly operator=(const HeapOnly&);int a;};
四、不能被继承的类,
派生类的继承会调用基类的构造函数,c++98将基类的构造函数私有化,派生类中调不到基类的构造函数。则无法继承,c++11;final关键字
//派生类中调不到基类的构造函数。则无法继承
class nonInherit{
public:static nonInherit GetInstance() {return nonInherit();}
private:nonInherit() {}
};
//c++11:关键字
class nonInheritC11 final
{};
五、只有一个对象的类
这是一种设计模式,叫做单例模式:
设计模式: 设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的 总结。为什么会产生设计模式这样的东西呢?就像人类历史发展会产生兵法。最开始部落之间打 仗时都是人拼人的对砍。后来春秋战国时期,七国之间经常打仗,就发现打仗也是有套路的,后 来孙子就总结出了《孙子兵法》。孙子兵法也是类似。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模 式使代码编写真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。
单例模式: 一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个 访问它的全局访问点,该实例被所有程序模块共享。比如在某个服务器程序中,该服务器的配置 信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再 通过这个单例对象获取这些配置信息,这种方式简化了在复杂环境下的配置管理。
单例模式有两种实现模式
饿汉(hungry模式):就是说不管你将来用不用,程序启动时就创建一个唯一的实例对象。
class Singleton {//hungry
public:static Singleton* GetInstance() {return &m_instance;}
private:Singleton() {};//c++98 防止拷贝Singleton(const Singleton &);Singleton& operator=(const Singleton &);//c++11Singleton(Singleton const&) = delete;Singleton& operator=(Singleton const&) = delete;static Singleton m_instance;// 程序入口前完成初始化
};static Singleton m_instance;
如果这个单例对象在多线程高并发环境下频繁使用,性能要求较高,那么显然使用饿汉模式来避 免资源竞争,提高响应速度更好。
懒汉模式
如果单例对象构造十分耗时或者占用很多资源,比如加载插件啊, 初始化网络连接啊,读取 文件啊等等,而有可能该对象程序运行时不会用到,那么也要在程序一开始就进行初始化, 就会导致程序启动时非常的缓慢。 所以这种情况使用懒汉模式(延迟加载)更好。
#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;int main(){thread t1([] {cout << Singleton::GetInstance()<<" " << endl; });thread t2([] {cout << Singleton::GetInstance()<<" " << endl; });t1.join();t2.join();cout << Singleton::GetInstance()<< endl;cout << Singleton::GetInstance()<< endl;return 0;
}