Error Dynamic_cast Is Not A Polymorphic Type
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the source type is not polymorphic dynamic_cast workings and policies of this site About Us Learn more about Stack
Dynamic_cast Example
Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Dynamic_cast Shared_ptr
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join
Dynamic_cast Vs Static_cast
them; it only takes a minute: Sign up C++: “… is not a polymorphic type” while using boost::dynamic_pointer_cast up vote 10 down vote favorite 1 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 c++ rtti to function template instantiation 'boost::shared_ptr #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_ptr = boost::shared_ptr( new B() ); std::list
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn c++ virtual destructor more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags c++ static_cast Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, c++ polymorphism helping each other. Join them; it only takes a minute: Sign up Getting “source type is not polymorphic” when trying to use dynamic_cast up vote 27 down vote favorite 5 struct A {}; struct B : A http://stackoverflow.com/questions/5831765/c-is-not-a-polymorphic-type-while-using-boostdynamic-pointer-cast {}; int main() { A* a = new B(); B* b = dynamic_cast(a); } gives: cannot dynamic_cast 'a' (of type 'struct A*') to type 'struct B*' (source type is not polymorphic) How can I make A polymorphic? I want to safely cast it to B. (One way is to add a dummy virtual function, but is there a better way?) c++ c++11 share|improve this question edited Jun 2 at 22:28 QPaysTaxes 3,29762047 asked Feb 27 '13 http://stackoverflow.com/questions/15114093/getting-source-type-is-not-polymorphic-when-trying-to-use-dynamic-cast at 14:05 Andrew Tomazos 26.6k1897182 2 If you don't need virtual functions, why do you want polymorphic types? static_cast works just fine here. –R. Martinho Fernandes Feb 27 '13 at 14:09 3 Adding to R. Martinho comment... if you feel that you need to perform casts you might already be in the wrong track. –David Rodríguez - dribeas Feb 27 '13 at 14:12 5 Virtual destructor for polymorphic type is not dummy - it saves you from UB if you decide to fix your memory leak by writing delete a; –Tadeusz Kopec Feb 27 '13 at 14:14 @R.MartinhoFernandes: I want it to return 0 if it isnt B. –Andrew Tomazos Feb 27 '13 at 14:15 @user1131467: That is why I said might already be... although in general casts tend to indicate flaws in the design. –David Rodríguez - dribeas Feb 27 '13 at 14:39 add a comment| 3 Answers 3 active oldest votes up vote 44 down vote accepted You need to make A polymorphic, which you can do by adding a virtual destructor or any virtual function: struct A { virtual ~A() {} }; Note that a polymorphic type should have a virtual destructor anyway, if you intend to safely call delete on instances of a derived type via a pointer to the base. share|improve this answer
int n; }; class b : public a { public: }; and I next do this: a *x = new b(); b *y = dynamic_cast(x); If I compile this, the compiler (msvc++ 2005) says: error C2683: 'dynamic_cast' : 'a' is not a polymorphic type and gnu g++ 3.2.3 says: cannot http://www.thecodingforums.com/threads/dynamic_cast-a-is-not-a-polymorphic-type.286044/ dynamic_cast `x' (of type `class a*') to type `class b*' ( source type is not polymorphic) I don't understand this. Why can't I dynamically cast a pointer of type base class to some derived class? Probably I have to tell the compiler that class a has https://groups.google.com/d/topic/microsoft.public.vc.language/rz7OtNo0hFI some subclasses. Anyone got an idea? Maurice Termeer Maurice Termeer, Oct 5, 2004 #1 Advertisements Sharad Kala Guest "Maurice Termeer" <> wrote in message > I don't understand this. Why can't I dynamically cast a pointer of type > base class to some derived class? Probably is not I have to tell the compiler > that class a has some subclasses. Anyone got an idea? You got to have at least one virtual function in your base class to use dynamic_cast or to make it polymorphic. Sharad Sharad Kala, Oct 5, 2004 #2 Advertisements Ron Natalie Guest "Sharad Kala" <> wrote in message news:... > > You got to have at least one virtual function in your base class to use > dynamic_cast or to make it polymorphic. > You need to have a polymorphic class to use dynamic_cast IN THIS error dynamic_cast is FASHION. The presence of the virtual function in the base class is what makes it "polymorphic" by DEFINITION. Ron Natalie, Oct 5, 2004 #3 John Harrison Guest "Maurice Termeer" <> wrote in message news:cjtve0$kc7$... > Hi, suppose i've got this: > > class a { > public: > int n; Add this virtual ~a() {} and it will work > }; > > class b : public a { > public: > }; > > and I next do this: > > a *x = new b(); > b *y = dynamic_cast(x); > > If I compile this, the compiler (msvc++ 2005) says: > > error C2683: 'dynamic_cast' : 'a' is not a polymorphic type > john John Harrison, Oct 5, 2004 #4 Rolf Magnus Guest Maurice Termeer wrote: > Hi, suppose i've got this: > > class a { > public: > int n; > }; > > class b : public a { > public: > }; > > and I next do this: > > a *x = new b(); > b *y = dynamic_cast(x); > > If I compile this, the compiler (msvc++ 2005) says: > > error C2683: 'dynamic_cast' : 'a' is not a polymorphic type > > and gnu g++ 3.2.3 says: > > cannot dynamic_cast `x' (of type `class a*') to type `class b*' ( > source type is not polymorphic) > > I don't understand this. Why can't I dynamically cast a pointer of type > base class to some derived class? Just as the compiler says: because your base class is not polymorphic, i.e. doesn't use dynamic typing. Therefore, dynamic type casting can't work. > Probabl
von GoogleAnmeldenAusgeblendete FelderNach Gruppen oder Nachrichten suchen