都是运算符:
static_cast
const_cast
dynamic_cast
reinterpret_cast
#include<iostream>
using namespace std;
int main()
{
//c++新类型转换运算符!!
//C语言:隐式类型转换(char/int/long/float/double)强制类型转换
//不安全:任何类型都可以互相转换,不做类型检查
/*int num = 5;
double b = num;
num = 'a';
int* p = #
char* ptr = (char*)p;//强制类型转换*/
//
//static_cast;相关类型转化;void*和其它类型指针之间的转化 父类和子类之间的转换
int num = 5;
double b = static_cast<double>(num);
char ch = static_cast<char>(num);
int* p_num = #
void* p = p_num;//void * 万能指针:接受任何类型指针的值
char* ptr = static_cast<char*>(p);
//reinterpret_cast:可以对任意类型的指针做转换,类似与C的强制类型转换(不安全)
ptr = reinterpret_cast<char*>(p_num);
//const_cast:去除指针和引用的const属性;(不安全,基本不用)
const int count = 5;
int const1 = 6;
const int* p_count = &count;
int* p_count1 = const_cast<int*>(p_count);
*p_count1 = 7;
const int& r_count = count;
int &r_count2 = const_cast<int &>(r_count);
//dynamic_cast(多态)
return 0;
}