Hol*_*yLa 15 c++ boost dynamic-cast boost-smart-ptr
我使用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
}
};
Run Code Online (Sandbox Code Playgroud)
lij*_*jie 19
请参阅使用boost :: shared_ptr的static_cast?
你需要使用dynamic_pointer_cast来获得适当的shared_ptr实例化.(对应a dynamic_cast)
| 归档时间: |
|
| 查看次数: |
31063 次 |
| 最近记录: |