当前位置:网站首页>Special class and type conversion
Special class and type conversion
2022-08-10 23:48:00 【Wind Shadow 66666】
目录
一、特殊类设计
二、类型转换
一、特殊类设计
只能在堆上创建对象
First you need to make its constructor private
The second is to use the static member function to get the object
同时,The copy construction also needs to be removed,Otherwise objects can also be created on the stack
只能在栈上创建对象
同上,Also need to make the constructor private
Then use the static member function to get the object
如果new时初始化,That is, there is a copy construction,That can also create objects on the heap,But copy construction cannot be eliminated here,Otherwise the object cannot be obtained,Because the above function is an anonymous object,Copy construction is also required
所以这里需要将new屏蔽掉
不能被拷贝
C++98
单例模式
概念:一个类只能创建一个对象,即单例模式,该模式可以保证系统中该类只有一个实例,并提供一个访问它的全 局访问点,该实例被所有程序模块共享
The following hungry mode and lazy mode are used to count the recursion times of quick sort as an example
饿汉模式
概念:提前准备好,随时可以吃,在main函数之前就创建好了单例对象,Programs can access this singleton object at any time
成员变量有3个,One is a static member object of the class,One is used to count the number of times,The other is used to save the interval separated by each quick sort
First you need to make the constructor private,And copy-constructed to delete
Also need to define the static member object of the class
Use a static member function to get the static member object of the class
The first function in the figure below returns the value of the number of times,The second comes to increase the number of times,The third is to save the interval tovector中
Call the above function in quicksort
测试用例
打印调用的次数,The quicksort is optimized,区间很小时,With the help of insertion sort,So the frequency is a little less
懒汉模式
概念:事先没有准备好,第一次访问时,才创建单例对象
和饿汉模式一样,也有3个成员变量,It's just that the object of this class is changed to an object pointer
Define the object pointer
获取对象
Except there is a difference in getting the object,The rest of the member functions are consistent with the hungry man mode!
There is a thread safety issue when the object is obtained for the first time above,比如t1和t2线程同时进入if语句,t2The thread first creates the object and returns,然后t2Increase the number of times,而t1又创建对象,times will change,所以会出现线程安全问题
The solution is to lock
Declare a lock member variable and define it
The situation in the figure below is double-checked locking,外面套一层ifStatements are for efficiency,比如t1The object has been created,而t2再来时,There will be no more locks
Singleton object recycling problem
一般懒汉的单例对象,不需要回收,因为进程正常结束,资源都会还给系统,There is no problem with only one system automatically reclaiming this object,If the singleton object is released and destructed,有一些要完成的动作,比如要记录日志等等,Then you can define a recycling class,It can be defined directly inside the class,如下图
Add a static member variable to the classgc,and out-of-class definitionsgc
This is written in the picture belowC++11中也可以,比较简洁,因为C++11进行了优化,所以static sInst对象构造初始化是线程安全的,但在C++98中多线程调用时,static sInst对象构造初始化并不能保证下线程安全
对比饿汉模式和懒汉模式
饿汉模式
优点:简单
缺点:
There is no control over the initialization order in which singleton objects are created,Suppose there are two singleton classesA和B,要求AThe singleton is created first,BCreated after the singleton,B的创建依赖A;
如果单例对象初始化很费时间,会导致程序启动慢,It's like it's stuck
懒汉模式
优点:
Corresponds to the two shortcomings of the hungry man mode
缺点
相对复杂一些,尤其还要控制线程安全的问题
二、类型转换
为什么C++A canonical type conversion will be proposed
因为C++兼容C,will also remainCHermit type conversions and casts,But this often leads to some problems in the program,如下图,endwill be promoted to unsigned integer,So it can't be less than0,所以C++Four type conversions are proposed
提示:隐士类型转换:相近类型--意义相似的类型
如下图,如果pa指向的父类对象,Then the conversion is unsuccessful,返回nullptr,如果pa指向的子类对象,Then the conversion is successful,返回对象指针
注意:
边栏推荐
猜你喜欢
随机推荐
MySQL数据库基础操作
ROS实验笔记之——UZH-FPV数据集的验证
多语种翻译-多语种翻译软件免费
Three-column layout implementation
Kioptrix Level 1 靶机wp
虎符CTF 2022 Quest-Crash Writeup
Unity--URP渲染管线实战教程系列之URP摄像机核心机制剖析
大厂面试热点:“热修复机制及常见的几个框架介绍”
翻译软件哪个准确度高【免费】
How to quickly grasp industry opportunities and introduce new ones more efficiently is an important proposition
7. yaml
三栏布局实现
安科瑞为工业能效行动计划提供EMS解决方案-Susie 周
C3604环保黄铜带
浅析工业互联网
HPb59-1铅黄铜
13. 内容协商
[C] the C language program design, dynamic address book (order)
十年架构五年生活-08 第一次背锅
2.0966 铝青铜板CuAl10Ni5Fe4铜棒