C ++:使用boost :: dynamic_pointer_cast时,“...不是多态类型”

时间:2011-04-29 12:03:38

标签: c++ compiler-errors shared-ptr dynamic-cast

为什么我会收到以下代码的以下错误?

1>C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(259): error C2683: 'dynamic_cast' : 'my_namespace::A' is not a polymorphic type
1>          D:\[location]\[header_filename].h(35) : see declaration of 'my_namespace::A'
1>          C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(522) : see reference to function template instantiation 'boost::shared_ptr<T>::shared_ptr<my_namespace::A>(const boost::shared_ptr<my_namespace::A> &,boost::detail::dynamic_cast_tag)' being compiled
1>          with
1>          [
1>              T=my_namespace::B
1>          ]
1>          [location]\[source_filename].cpp(217) : see reference to function template instantiation 'boost::shared_ptr<T> boost::dynamic_pointer_cast<my_namespace::B,striker::A>(const boost::shared_ptr<my_namespace::A> &)' being compiled
1>          with
1>          [
1>              T=my_namespace::B
1>          ]
1>C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(260): fatal error C1903: unable to recover from previous error(s); stopping compilation

C ++代码或多或少如下:

#include <list>
#include "boost/pointer_cast.hpp"
#include "boost/shared_ptr.hpp"

struct A
{
public:
    A(const MyEnum an_enum_, const int an_int_) :
        an_enum(an_enum_),
        an_int(an_int_)
    {}

    const MyEnum an_enum;
    const int an_int;
};

struct B : public A {
public:
    B(const int some_int_, const MyStruct &a_struct_) :
        A(ENUM_OPTION_A, an_int_),
        a_struct(a_struct_)
    {}

    const MyStruct a_struct;
};


// Ussage in some function:
// ...
boost::shared_ptr<A> a_ptr = boost::shared_ptr<A>( new B() );
std::list<boost::shared_ptr<A>> a_list;
a_list.push_back(a_ptr);
// ...
boost::shared_ptr<A> a_ptr2 = a_list.front();
boost::shared_ptr<B> b_ptr = boost::dynamic_pointer_cast<B>(a_ptr2); // <-- error here
// ...

3 个答案:

答案 0 :(得分:57)

dynamic_cast仅适用于多态类。 polymorphic类是至少具有一个virtual函数的类,即使它是析构函数。

//polymorphic classes
struct A
{
   virtual ~A(); //even virtual destructor makes a class polymorphic!
};
struct B : A
{
   void f();
};

//non-polymorphic classes    
struct C
{
   ~C(); //not virtual
};

struct D : C
{
   void f(); //not virtual either
};

在上面的代码中,AB是多态类,但CD不是。

A *pA = new B();
B *pB = dynamic_cast<B*>(pA); //okay

C *pC = new D();
D *pD = dynamic_cast<D*>(pC);  //error -  not polymorphic class

请注意,在dynamic_cast中,只有源类型需要多态才能进行编译。如果目标不是多态,则dynamic_cast将返回空指针。

D *pD = dynamic_cast<D*>(pA);  //okay - source (pA) is polymorphic

if ( pD )  
      cout << "pD is not null" ;
else 
      cout << "pD is null";

输出:

pD is null

在线演示:https://web.archive.org/web/20000000000000/http://www.ideone.com/Yesxc

答案 1 :(得分:4)

'dynamic_cast' : 'my_namespace::A' is not a polymorphic type因为它没有定义或继承单个virtual函数。只需添加一个虚拟析构函数就可以了。

dynamic_cast仅适用于此类&#39;多态&#39;类型。

答案 2 :(得分:2)

struct A没有虚方法(甚至不是析构函数),因此你不能dynamic_cast A* - 只能使用指向具有至少一个虚拟成员函数的类型的指针dynamic_cast开启。 boost::dynamic_pointer_cast在内部dynamic_cast,它受到相同的要求。