当前位置:网站首页>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

将拷贝构造函数与赋值运算符重载只声明不定义,And set its access to private
C++11
Add one directly after it=delete即可
不能被继承
C++98
Make the constructor private
C++11
Add one after the classfinal

单例模式

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

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 

提示:隐士类型转换:相近类型--意义相似的类型

static_cast
static_cast用于非多态类型的转换(静态转换),编译器隐式执行的任何类型转换都可用static_cast,但它不能用于两个不相关的类型进行转换,That is, it is used for conversion of similar types
reinterpret_cast
reinterpret_cast操作符通常为操作数的位模式提供较低层次的重新解释,用于将一种类型转换为另一种不同的类型,如下图,Converts a function type with parameters and return value to a function type with no parameters and return value,非常的BUG,下面转换函数指针的代码是不可移植的,所以不建议这样用
C++不保证所有的函数指针都被一样的使用,所以这样用有时会产生不确定的结果
const_cast
const_castThe most common use is to delete variablesconst属性,方便赋值,如下图,a的constattribute is removed,打印*p变为了3,而打印a依然为2,则是因为constThe attribute being removed conflicts with the compiler's optimizations,The memory is stored3,而aNo value is fetched from memory,Hence a misjudgment
解决方式,is to add one in frontvolatile关键字,This tells the compiler not to optimize,Every time you go to the memory to get the value
注意:C++Separate this out,It is to remind the user to pay attentionconstAfter the attribute is deleted,会被修改.Be careful and misjudgment of compiler optimization conflicts 
dynamic_cast
dynamic_cast用于将一个父类对象的指针/引用转换为子类对象的指针或引用(动态转换)
向上转型:子类对象指针/引用->父类指针/引用(不需要转换,赋值兼容规则)
向下转型:父类对象指针/引用- >子类指针/引用(用dynamic_cast转型是安全的)
如下图,哪怕用reinterpret_castYou can't assign a subclass object to a parent class object either

 

 

如下图,如果pa指向的父类对象,Then the conversion is unsuccessful,返回nullptr,如果pa指向的子类对象,Then the conversion is successful,返回对象指针

 

注意:

dynamic_cast只能用于含有虚函数的类
dynamic_cast会先检查是否能转换成功,能成功则转换,不能则返回0
explicit
explicitThe keyword prevents implicit conversions from happening through the conversion constructor​​​​​​​ 
原网站

版权声明
本文为[Wind Shadow 66666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102330310551.html