shared_ptr
の基本クラスを使用しているときに、逆参照するときに派生クラスのメソッドを呼び出すことができないようです。私は、コードは私よりもより冗長になると信じて:boost :: 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
}
};
あなたのサンプルがコンパイル可能であっても、もちろん批判的な行がコメントされている場合に役立ちます。 –