1.C++中的类型转换分为四种形式:
C语言中的转换都是没有意义的,就是说C语言中的强转都是(int) (int*)这样的类型,你不知道它这么转的意义是什么,意义不够明确,所以C++中给出了4种类型转换
①static_cast:值的转换,相当于C语言中的强转,这个转换意义很明确就是将float类型转成整型
int a = 10;
float ft = 12.23;
a = static_cast<int>(ft);//转成int
ft = static_cast<float>(a);//转成float
②const_cast:是去常性强转,C语言的这种方式int* p = (int*)&a;的意义并不明确,而C++中的int* p = const_cast<int*>(&a);意义非常明确,就是去常性
const int a = 10;
int* p = (int*)&a;//C语言方式
int* p = const_cast<int*>(&a);//C++方式
③reinterpret_cast:是对指针进行重新解释的转换
int a = 10;
int* p = &a;
char* cp = (char*)&a;//C语言方式
char* cp = reinterpret_cast<char*>(&a);
④dynamic_cast:动态强制类型转换:将子对象的地址强转成父指针
Object* op = NULL;
Base* bp = NULL;
Object obj;
Base base;
op = &base;//C方式ok
bp = &Object;//C方式,error
op = dynamic_cast<Object*>(&base);//C++,ok
bp = dynamic_cast<Object*>(&base);//error
就是说C++使类型转换的意义更明确了
2.std::move移动构造函数的内部是怎样实现
my_remove_reference可以移除引用特质,也就是说无论传进来的是int类型,还是int&类型,还是int&&类型,type永远都是int类型
template<class _Ty>
struct my_remove_reference
{
using type = _Ty;
using Const_thru_ref_type = const _Ty;
};
template<class _Ty>
struct my_remove_reference<_Ty&>
{
using type = _Ty;
using Const_thru_ref_type = const _Ty&;
};
template<class _Ty>
struct my_remove_reference<_Ty&&>
{
using type = _Ty;
using Const_thru_ref_type = const _Ty&&;
};
template<class