Hello,
I am using boost::any to have polymorphic types, I need to be able to cast an object to its base type.
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);
}
The code of the main function throws the following error at runtime:
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
If I change the static_cast in the boost::any_cast code for reinterpret_cast it seems to work. However I am not sure about the consequences of that.
Do you have any ideas?