我在使用基類的shared_ptr
時遇到問題,我無法在解引用它時調用派生類的方法。我相信代碼會比我更詳細:boost :: shared_ptr和dynamic cast
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
}
};
如果你的例子是可編譯的,那當然會對這條有問題的代碼段進行評論。 – 2010-11-25 14:26:04