平时用的不多,但是比较重要的知识点
下面是概念框架和代码验证
#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); //去const
A * 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");
}
结果并不重要,这里代码主要是示范用法