目录

拷贝只会放在两个场景中:拷贝构造函数以及赋值运算符重载,因此想要让一个类禁止拷贝,
只需让该类不能调用拷贝构造函数以及赋值运算符重载即可。
将拷贝构造函数与赋值运算符重载只声明不定义,并且将其访问权限设置为私有即可。
这样做的原因有两个:
-
- class Bancopy
- {
- public:
- Bancopy(int x)
- :_x(x)
- {
- }
- private:
- Bancopy(Bancopy& obj);
- Bancopy& operator=(Bancopy& obj);
- private:
- int _x;
- };
C++11扩展delete的用法,delete除了释放new申请的资源外,如果在默认成员函数后跟上
=delete,表示让编译器删除掉该默认成员函数。
- class Bancopy
- {
- public:
- Bancopy(int x)
- :_x(x)
- {
- }
-
- Bancopy(Bancopy& obj) = delete;
- Bancopy& operator=(Bancopy& obj) = delete;
- private:
- int _x;
- };
- //思路一,将析构函数私有化
- class HeapOnly
- {
- public:
- HeapOnly(int x)
- :_x(x)
- {
- }
- void Destory()
- {
- cout << "delete this" << endl;
- delete this;
- }
- private:
- ~HeapOnly()
- {
- }
- private:
- int _x;
- };
测试代码:
- int main()
- {
- //HeapOnly t1(100);//对象无法自动调用析构函数
- //static HeapOnly t2(100);//对象无法自动调用析构函数
-
- //手动释放对象空间
- HeapOnly* t = new HeapOnly(100);
- t->Destroy();
-
- return 0;
- }

- //思路二,将构造函数私有化
- class HeapOnly
- {
- public:
- static HeapOnly* CreateObj(int x)
- {
- return new HeapOnly(x);
- }
-
- void Destroy()
- {
- cout << "delete this" << endl;
- delete this;
- }
-
- HeapOnly(HeapOnly& Obj) = delete;
- HeapOnly& operator=(HeapOnly& Obj) = delete;
-
- private:
- HeapOnly(int x)
- :_x(x)
- {
- }
- private:
- int _x;
- };
测试代码:
- int main()
- {
- //手动释放对象空间
- HeapOnly* t = HeapOnly::CreateObj(100);
-
- //HeapOnly t2 = (*t); error
- //t2 = *t; error
-
- t->Destroy();
-
- return 0;
- }

方法一:同上将构造函数私有化,然后设计静态方法创建对象返回即可。
- class StackOnly
- {
- public:
- static StackOnly CreateObj(int x)
- {
- return StackOnly(x);
- }
-
- StackOnly& operator=(StackOnly& Obj) = delete;
-
- private:
- StackOnly(int x)
- :_x(x)
- {
- }
- private:
- int _x;
- };
缺陷:对于静态对象我们没有办法阻止创建,因为我们通过静态的CreateObj也是需要传值返回的。如果将拷贝构造封住,换成移动构造,可以将普通的对象赋值给静态的对象拦截住,但是对于move之后的普通对象赋值给静态对象。
- class StackOnly
- {
- public:
- static StackOnly CreateObj(int x)
- {
- return StackOnly(x);
- }
-
- StackOnly(StackOnly&& Obj)
- {
- _x = Obj._x;
- }
-
- StackOnly(StackOnly& Obj) = delete;
- StackOnly& operator=(StackOnly& Obj) = delete;
-
- private:
- StackOnly(int x)
- :_x(x)
- {
- }
- private:
- int _x;
- };
-
- int main()
- {
- StackOnly t = StackOnly::CreateObj(100);
- //StackOnly* pt = new StackOnly(200); error
-
- //static StackOnly t1 = t; //error
- static StackOnly t1 = move(t);//没办法阻止
-
- return 0;
- }
C++98中构造函数私有化,派生类中调不到基类的构造函数。则无法继承。派生类在对于基类的那部分数据回去调用基类的构造函数来完成,如果基类的构造函数变成私有,那么派生类就无法调用构造。接入派生类无法创建对象。
- class base
- {
- public:
- static base GetInstance()
- {
- return base();
- }
- private:
- base()
- {}
- };
final关键字,final修饰类,表示该类不能被继承。
- class base final
- {
- public:
-
- base()
- {}
- private:
-
- };
设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的
总结。为什么会产生设计模式这样的东西呢?就像人类历史发展会产生兵法。最开始部落之间打
仗时都是人拼人的对砍。后来春秋战国时期,七国之间经常打仗,就发现打仗也是有套路的,后
来孙子就总结出了《孙子兵法》。孙子兵法也是类似。
使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模
式使代码编写真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。
一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个
访问它的全局访问点,该实例被所有程序模块共享。比如在某个服务器程序中,该服务器的配置
信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再
通过这个单例对象获取这些配置信息,这种方式简化了在复杂环境下的配置管理。
就是说不管你将来用不用,程序启动时就创建一个唯一的实例对象。
可能会导致进程启动慢,且如果有多个单例类对象实例启动顺序不确定。
- //单例模式——饿汉模式
- class Singleton
- {
- public:
- static Singleton* GetSingleton()
- {
- return singleton;
- }
-
- vector
&get_sing_list() - {
- return _sing_list;
- }
-
- void showlist()
- {
- for (auto e : _sing_list)
- {
- cout << e << endl;
- }
- }
- private:
- //构造函数
- Singleton()
- {}
- Singleton(Singleton const&) = delete;
- Singleton& operator=(Singleton const&) = delete;
- private:
- vector
_sing_list; - static Singleton* singleton;
- };
- //初始化的是类内部成员,可以访问私有的构造函数
- //在没有进入主函数的时候,单例对象已经实例化完成
- Singleton* Singleton::singleton = new Singleton;
测试代码:
- int main()
- {
- Singleton* singleton = Singleton::GetSingleton();
- singleton->get_sing_list().push_back("《冰雨》——刘德华");
- singleton->get_sing_list().push_back("《月光》——胡彦斌");
- singleton->get_sing_list().push_back("《倒带》——周杰伦");
- singleton->get_sing_list().push_back("《呓语》——毛不易");
-
- singleton->showlist();
-
- return 0;
- }

如果这个单例对象在多线程高并发环境下频繁使用,性能要求较高,那么显然使用饿汉模式来避
免资源竞争,提高响应速度更好。
如果单例对象构造十分耗时或者占用很多资源,比如加载插件啊, 初始化网络连接啊,读取
文件啊等等,而有可能该对象程序运行时不会用到,那么也要在程序一开始就进行初始化,
就会导致程序启动时非常的缓慢。 所以这种情况使用懒汉模式(延迟加载)更好。
优点:第一次使用实例对象时,创建对象。进程启动无负载。多个单例实例启动顺序自由控
制。
- //单例模式——懒汉模式
- class Singleton
- {
- public:
- static Singleton* GetSingleton()
- {
- if (singleton == nullptr)//防止因为每次创建对象都要加锁带来的性能上的损耗
- {
- _mutex.lock();//防止多线程创建的时候,出现并发问题
- if (singleton == nullptr)
- {
- singleton = new Singleton;
- }
- _mutex.unlock();
- }
- return singleton;
- }
-
- vector
&get_sing_list() - {
- return _sing_list;
- }
-
- void showlist()
- {
- for (auto e : _sing_list)
- {
- cout << e << endl;
- }
- }
- private:
- //构造函数
- Singleton()
- {
- i++;
- }
- Singleton(Singleton const&) = delete;
- Singleton& operator=(Singleton const&) = delete;
- private:
-
- static mutex _mutex;
- vector
_sing_list; - static Singleton* singleton ;
- };
-
- Singleton* Singleton::singleton = nullptr;
- mutex Singleton::_mutex;