#include <iostream>
using namespace std;
class X{
public:
virtual void f(){}
};
class Y {
public:
virtual void g() {}
};
int main()
{
X * x = new X();
Y* y = dynamic_cast<Y*>(x); //A
// Y* y = static_cast<Y*>(x); //B
cout << y << endl;
}
Run Code Online (Sandbox Code Playgroud)
A编译而不编译B.我理解为什么B不编译,但为什么A编译虽然X和Y完全不相关的类型?