: 는 " 'Cloneable을 *'로 변환 할 수없는 'AClass *'"내 지식을 당로서,이 거래 라인 1 에 컴파일 시간과 실행 시간 다형성에 대한 개념을 가지고 있습니다. 그러나 이것에 대해서는 구체적인 이유가 없습니다. 기본적으로다형성 : 컴파일시 상기 code.Need 오류에 대한 설명
struct Cloneable
{
virtual Cloneable* clone()
{
cout << "Cloneable";
return new Cloneable;
}
virtual ~Cloneable() {}
};
struct AClass : public Cloneable
{
virtual AClass* clone()
{
cout << "AClass";
return new AClass;
}
};
int main()
{
Cloneable* s1 = new AClass;
AClass* s2 = s1->clone(); //Line 1
return 0;
}
@muhmud ...이 코드는 내가 static_cast를 (사용하는 경우) 잘 작동합니다 (파생 클래스 포인터 유형으로 기본 클래스 포인터 타입 캐스팅) .Modifying 라인 1과 : AClass * S2 = static_cast (S1 -> clone()); 내가 정말로 알고 싶은 것은 컴파일러가이 코드를 해석하는 방법입니다. –
user2100866
@ user2100866 - 이걸 봐야합니다 - http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast. 'dynamic_cast()'는 정의되지 않은 동작을 방지 할 수있는 다운 캐스팅 중에 런타임 검사를 수행합니다. – muhmud