我正在使用boost :: any有多態類型,我需要能夠將一個對象轉換爲它的基類型。 class A {
public:
int x;
virtual int foo()= 0;
};
class B : public A {
public:
int foo() {
return x + 1;
}
};
我正在使用boost::any來存儲指針,並想知道是否有 提取多態數據類型的方法。 下面是一個理想情況下我想要做的簡單示例,但目前不起作用。 struct A {};
struct B : A {};
int main() {
boost::any a;
a = new B();
boost::any_cast< A* >(a);
}
這失敗了,因爲a