9.四种转换类型

来源:互联网 发布:中老年交友软件 编辑:程序博客网 时间:2024/06/07 02:37

平时用的不多,但是比较重要的知识点

下面是概念框架和代码验证


#include <iostream>using namespace std;class A {public:virtual ~A() {}};class B :public A {};void main(){int a = 0,c = 2;const int *d = &c;double b = 1;int * p = &a;double * s = &b;a = static_cast<int>(b);c = reinterpret_cast<int>(p);//获得p指针的地址s = reinterpret_cast<double*>(p);//拷贝指针中的内容,如果类型不同,并不能用转换目标指针指示内容int * d1 = const_cast<int *>(d);//去constA * p1 = new A();B * p2 = new B();p2 = dynamic_cast<B*>(p1);//向下转换p1 = dynamic_cast<A*>(p2);//向上cout << a << *p << c << *s <<endl;system("pause");}

结果并不重要,这里代码主要是示范用法

原创粉丝点击