C++: quot;... is not a polymorphic typequot; while using boost::dynamic_pointer_cast(C++:“……不是多态类型;使用 boost::dynamic_pointer_cast 时)
问题描述
为什么我会收到以下代码的以下错误?
Why do I receive the following error for the following code?
1>C:Libsoost_1_44oost/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:Libsoost_1_44oost/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:Libsoost_1_44oost/smart_ptr/shared_ptr.hpp(260): fatal error C1903: unable to recover from previous error(s); stopping compilation
C++ 代码大致如下:
The C++ code is more or less the following:
#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
// ...
推荐答案
dynamic_cast 仅适用于多态类.而polymorphic类是至少有一个virtual函数的类,即使是析构函数.
dynamic_cast works ONLY with polymorphic class. And polymorphic class is that which has atleast one virtual function, even be it the destructor.
//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不是.
In the above code, A and B are polymorphic classes, but C and D are not.
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 将返回空指针.
Note that in dynamic_cast, only the source type need to be polymorphic in order to compile. If the destination isn't polymorphic, then dynamic_cast will return null pointer.
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
这篇关于C++:“……不是多态类型";使用 boost::dynamic_pointer_cast 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++:“……不是多态类型";使用 boost::dynamic_pointer_cast 时
基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
