C++:“...不是多态类型”使用 boost::dynamic_pointer_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
// ...
Why do I receive the following error for the following code?
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
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
// ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
dynamic_cast
仅适用于多态类。而多态类是指至少具有一个虚拟函数的类,即使它是析构函数。在上面的代码中,
A
和B
是多态类,但C
和D
不是。请注意,在
dynamic_cast
中,只有源类型需要是多态才能编译。如果目标不是多态,则dynamic_cast
将返回空指针。输出:
在线演示:https://web.archive。 org/web/20000000000000/http://www.ideone.com/Yesxc
dynamic_cast
works ONLY with polymorphic class. Andpolymorphic
class is that which has atleast onevirtual
function, even be it the destructor.In the above code,
A
andB
are polymorphic classes, butC
andD
are not.Note that in
dynamic_cast
, only the source type need to be polymorphic in order to compile. If the destination isn't polymorphic, thendynamic_cast
will return null pointer.Output:
Online demo: https://web.archive.org/web/20000000000000/http://www.ideone.com/Yesxc
'dynamic_cast' : 'my_namespace::A' 不是多态类型
,因为它不定义或继承单个虚拟
函数。只需添加一个虚拟析构函数就可以了。dynamic_cast
仅适用于此类“多态”类型。'dynamic_cast' : 'my_namespace::A' is not a polymorphic type
because it doesn't define or inherit a singlevirtual
function. Just add a virtual destructor and you'll be fine.dynamic_cast
works only for such 'polymorphic' types.struct A
没有虚拟方法(甚至没有析构函数),因此您无法从A*
进行dynamic_cast
- 只能指向带有 at 的类型至少可以使用dynamic_cast
一个虚拟成员函数。boost::dynamic_pointer_cast
在内部执行dynamic_cast
,它遵循相同的要求。struct A
has no virtual methods (not even a destructor), so you can'tdynamic_cast
fromA*
- only pointers to types with at least one virtual member function can be useddynamic_cast
on.boost::dynamic_pointer_cast
doesdynamic_cast
inside, to it's subject to same requirements.