C++ C++;:&引用;。。。不是多态类型”;使用boost::dynamic\u pointer\u cast时
为什么我收到以下代码的以下错误C++ C++;:&引用;。。。不是多态类型”;使用boost::dynamic\u pointer\u cast时,c++,compiler-errors,shared-ptr,dynamic-cast,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>
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
1>C:\Libs\boost\u 1\u 44\boost/smart\u ptr/shared\u ptr.hpp(259):错误C2683:'dynamic\u cast':'my\u namespace::A'不是多态类型
1> D:\[location]\[header\u filename].h(35):请参见“my\u namespace::A”的声明
1> C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(522):请参阅正在编译的函数模板实例化“boost::shared_ptr::shared_ptr(const boost::shared_ptr&,boost::detail::dynamic_cast_tag)”
1> 与
1> [
1> T=我的_名称空间::B
1> ]
1> [location]\[source\u filename].cpp(217):请参阅正在编译的函数模板实例化“boost::shared\u ptr boost::dynamic\u pointer\u cast(const boost::shared\u ptr&)”的参考
1> 与
1> [
1> T=我的_名称空间::B
1> ]
1> C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(260):致命错误C1903:无法从以前的错误中恢复;停止编译
<> 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
// ...
#包括
#包括“boost/pointer\u cast.hpp”
#包括“增压/共享_ptr.hpp”
结构A
{
公众:
A(常数MyEnum an_enum,常数int an_int):
一个枚举(一个枚举),
an_int(an_int)
{}
圣母院;
常数和单位;
};
结构B:公共A{
公众:
B(const int some_int,const MyStruct&a_struct):
A(枚举选项A,一个整数),
结构(结构)
{}
const MyStruct a_struct;
};
//在某些功能中使用:
// ...
boost::shared_ptr a_ptr=boost::shared_ptr(新的B());
std::列表a_列表;
a_列表。向后推(a_ptr);
// ...
boost::shared_ptr a_ptr2=a_list.front();
boost::shared_ptr b_ptr=boost::dynamic_pointer_cast(a_ptr2);// 'dynamic_cast':'my_namespace::A'不是多态类型
,因为它不定义或继承单个虚拟
函数。只需添加一个虚拟析构函数,就可以了
dynamic\u cast
仅适用于此类“多态”类型。dynamic\u cast
仅适用于多态类。多态类是至少有一个虚拟函数的类,即使它是析构函数
//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
};
在上述代码中,A
和B
是多态类,但C
和D
不是多态类
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
输出:
pD is null
在线演示:结构A
没有虚拟方法(甚至没有析构函数),因此您不能从A*
中使用动态强制转换
——只能使用指向至少具有一个虚拟成员函数的类型的指针boost::dynamic_pointer_cast
在内部执行dynamic_cast
操作,以满足相同的要求。+1但是,为什么您对D::f()进行了注释;如果D::f()
是虚拟的,那么D
将是多态类,尽管C
仍然是非多态类。true。但是我们对C
和D
之间的dynamic\u cast
感兴趣。因此,无论D
是否是多态的,@iammilind:如果D
不是多态的,那么dynamic_-cast(pC)
将返回NULL,如果pC
是多态的,或者pC
不是多态的,那么代码将不会编译。
pD is null