【发布时间】:2010-11-25 13:55:41
【问题描述】:
我在使用基类的shared_ptr 时遇到问题,在取消引用它时我似乎无法调用派生类的方法。我相信代码会比我更冗长:
class Base : public boost::enable_shared_from_this<Base>
{
public:
typedef boost::shared_ptr<BabelNet> pointer;
};
class Derived : public Base
{
public:
static pointer create()
{
return pointer(new Derived);
}
void anyMethod()
{
Base::pointer foo = Derived::create();
// I can't call any method of Derived with foo
// How can I manage to do this ?
// is dynamic_cast a valid answer ?
foo->derivedMethod(); // -> compilation fail
}
};
【问题讨论】:
-
如果您的示例是可编译的,这将有所帮助,当然有问题的行已被注释。
标签: c++ boost dynamic-cast boost-smart-ptr