目录
- void Test ()
- {
- int i = 1;
- // 隐式类型转换
- double d = i;
- printf("%d, %.2f\n" , i, d);
- int* p = &i;
- // 显示的强制类型转换
- int address = (int) p;
- printf("%x, %d\n" , p, address);
- }

c语言隐式类型转换会发生的一些问题
- int main()
- {
- int i = 5; size_t j = 0;
- while (i >= j)
- {
- i--;
- if (i == -10)
- break;
- cout << i << " ";
- }
- return 0;
- }

所以C++提出了中类型转换来规范
static_cast对应的是C语言中的隐式类型转换,支持相近类型的转换,非相近类型编译不通过;

- void Test1()
- {
- int i = 1;
- // sattic_cast
- double d = static_cast<double>(i);
- printf("%d, %.2f\n", i, d);
- }
- int main()
- {
- Test1();
- return 0;
- }

reinterpret_cast对应的是C语言中的显示类型转换,支持非相近类型的转换
- void Test1()
- {
- int i = 1;
- // reinterpret_cast
- int* p = &i;
- int address = reinterpret_cast<int>(p);
- printf("%x, %d\n", p, address);
- }
- int main()
- {
- Test1();
- return 0;
- }

const_cast最常用的用途就是删除变量的const属性,方便赋值
- int main()
- {
- const int a = 5;
- int* p = const_cast<int*>(&a);
- *p = 10;
- cout << a << endl;
- cout << *p << endl;
-
- return 0;
- }
上面代码执行结果

原因:编译器都被const修饰的变量有优化;不会取内存中取,会直接使用常量5;
关键字volatile:不让编译器优化,去内存中取值

常量区禁止写入,编译好的指令在常量区
- volatile const int a = 0;//常量区不能写入,编译好的指令在常量区
- int main()
- {
- int* p = const_cast<int*>(&a);
- *p = 10;
- cout << a << endl;
- cout << *p << endl;
-
- return 0;
- }
要求:
需求:如果pa是指向父类对象,那么不做任何处理,如果pa是指向子类对象,那么请转回子类,并打印子类对象地址
- class A
- {
- virtual void f() {}
- public:
- };
-
- class B : public A
- {
- public:
- };
- void func(A* pa)//切片
- {
- // dynamic_cast--如果pa指向的父类对象,那么则转换不成功,返回nullptr
- // dynamic_cast--如果pa指向的子类对象,那么则转换成功,返回对象指针
- B* pb1 = dynamic_cast(pa);
- if (pb1 == nullptr)
- {
- cout << "转换失败" << endl;
- }
- else
- {
- cout << "pb1:" << pb1 << endl;
- }
- }
-
- int main()
- {
- A aa;
- B bb;
- func(&aa);
- func(&bb);
- }
