#2
rjsp2023-05-13 22:09
|
程序代码:
#include <iostream>
class moveobject
{
void virtual func( )
{
}
};
class human:public moveobject
{
public:
void func( ) override
{
}
};
class monster :public moveobject
{
public:
void func( ) override
{
}
};
int main()
{
moveobject* pcture = new human();
auto pmonster = dynamic_cast<monster*>(pcture);
auto phuman = dynamic_cast<human*> (pcture);
std::cout << phuman << " " << pmonster<<std::endl;
system("pause");
}
为什么dynamic对于一个指针只能进行一次转换呢?这里第一次转换pcture转换成功了,第二次没有成功,为什么呢?它是将指针本身进行了转换并改变了它的类型么?