小编Hol*_*yLa的帖子

boost :: shared_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)

c++ boost dynamic-cast boost-smart-ptr

15
推荐指数
1
解决办法
3万
查看次数

标签 统计

boost ×1

boost-smart-ptr ×1

c++ ×1

dynamic-cast ×1