【发布时间】:2014-09-13 08:40:16
【问题描述】:
如果我将原始指针指向派生类,我想从 boost 变体中获取指向基类的指针。有没有办法实现这一点。以下代码不起作用。
class A{ public: virtual ~A(){}}; class B : public A{};
typedef boost::variant<A*,B*> MyVar;
MyVar var = new B;
A* a = boost::get<A*> (var); // the following line throws exception
也许有人知道如何编写我自己的 get 函数,该函数将测试请求的类型是否是变体中存储类型的基类,然后进行适当的转换
【问题讨论】:
标签: c++ boost boost-variant