다형성 타입을 사용하기 위해 boost :: any를 사용하고 있는데, 객체를 기본 유형으로 캐스팅 할 수 있어야합니다.boost :: any_cast (C++ 라이브러리)를 사용하여 기본 유형으로 변환하는 방법은 무엇입니까?
class A {
public:
int x;
virtual int foo()= 0;
};
class B : public A {
public:
int foo() {
return x + 1;
}
};
int main() {
B* bb = new B();
boost::any any = bb;
bb->x = 44;
A* aa = boost::any_cast<A*>(any);
}
주요 기능의 코드는 실행시 다음과 같은 오류가 발생합니다 :
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >'
what(): boost::bad_any_cast: failed conversion using boost::any_cast
Abort trap
내가 부스트 ::이 작동하는 것 같다 reinterpret_cast에 대한 any_cast 코드에서 static_cast를 변경하는 경우. 그러나 나는 그 결과에 대해 확신하지 못한다.
의견이 있으십니까?
값 별 저장 개체를 삭제하고 삭제). – Sergei