qfedu-cpp-level/day7/d18.cpp

40 lines
534 B
C++
Raw Permalink Normal View History

2023-08-01 18:51:53 +08:00
#include <iostream>
using namespace std;
class A
{
public:
int x;
void show()
{
cout << "A show() x = " << x << endl;
}
};
class B
{
public:
int x;
void show()
{
cout << "B show() x = " << x << endl;
}
};
int main()
{
A *a = new A();
a->x = 100;
B *b = new B();
b->x = 200;
// B *b2 = static_cast<B *>(a); // error
// B *b3 = dynamic_cast<B *>(a); // error
B *b4 = reinterpret_cast<B *>(a); // 输出: B show() x = 100
b4->show();
return 0;
}