Error Cannot Dynamic_cast Source Type Is Not Polymorphic
Contents |
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
Dynamic_cast Example
site About Us Learn more about Stack Overflow the company Business Learn more dynamic_cast shared_ptr about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Downcasting In C++
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Can't downcast dynamic_cast vs static_cast because class is not polymorphic? up vote 20 down vote favorite 5 Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic. Example: Class A(){ int a; int getA(){return a;}; } Class B(): A(){ int b; int getB(){return b;}; } In another class we are trying to downcast from an A object to c++ rtti a B object: A *a; B *b = dynamic_cast(a) but this gives the following error: cannot dynamic_cast ... (source type is polymorphic) c++ inheritance polymorphism vtable share|improve this question asked Dec 12 '11 at 4:22 wbarksdale 3,25683974 1 a is not a pointer. Is this how it is in your code? –littleadv Dec 12 '11 at 4:26 Sorry, a is in fact a pointer. –wbarksdale Dec 12 '11 at 4:26 And is the error compile time or run time? If run time, then IMHO its the expected behavior. –littleadv Dec 12 '11 at 4:27 its a compile time error. –wbarksdale Dec 12 '11 at 4:28 2 Is it just me or the class definitions are not c++? (everyone else seems to ignore that) –Dani Dec 12 '11 at 4:46 | show 3 more comments 5 Answers 5 active oldest votes up vote 41 down vote accepted Syntax errors non-withstanding, you cannot dynamic_cast a non-polymorphic type. static_cast is the cast you would use in this case, if you know that it is in fact an object of the target type. The reason why: static_c
here for a quick overview of the site Help Center Detailed answers
C++ Virtual Destructor
to any questions you might have Meta Discuss the workings dynamic casting c++ and policies of this site About Us Learn more about Stack Overflow the company Business
C++ Polymorphism Example
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack http://stackoverflow.com/questions/8469900/cant-downcast-because-class-is-not-polymorphic Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join 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 http://stackoverflow.com/questions/5831765/c-is-not-a-polymorphic-type-while-using-boostdynamic-pointer-cast 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 #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 =
Post your question and get tips & solutions from a community of 418,505 IT Pros & Developers. It's quick & easy. dynamic_cast: 'a' is not a polymorphic https://bytes.com/topic/c/answers/134946-dynamic_cast-not-polymorphic-type type P: n/a Maurice Termeer Hi, suppose i've got this: class a { http://www.drdobbs.com/cpp/twisting-the-rtti-system-for-safe-dynami/229401004 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 error cannot 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 some subclasses. Anyone got an idea? Maurice Termeer Jul 22 '05 #1 Post Reply Share this Question 4 Replies P: n/a Sharad Kala "Maurice Termeer"
DevJolt Awards Channels▼ CloudMobileParallel.NETJVM LanguagesC/C++ToolsDesignTestingWeb DevJolt Awards C/C++ Tweet Permalink Twisting the RTTI System for Safe Dynamic Casts of void* in C++ By Cassio Neri, April 05, 2011 A safe mechanism to dynamically cast void* to typed pointers at runtime In this article, I'll discuss a safe mechanism to dynamically cast void* to typed pointers at runtime, with examples of how to use void* for heterogeneous storage at runtime rather than polymorphism or alternatives like boost::any [1]. More Insights White Papers The Role of the WAN in Your Hybrid Cloud Rogue Wave Tools and Libraries for Big Data More >>Reports Hard Truths about Cloud Differences SaaS 2011: Adoption Soars, Yet Deployment Concerns Linger More >>Webcasts Catch the Security Breach Before It's Out of Reach Advanced Threat Protection For Dummies ebook and Using Big Data Security Analytics to Identify Advanced Threats Webcast More >> Introduction The following problem arose during the development of KeyValue [2]. One of this library's responsibilities consists of mapping string names to addresses (i.e. pointers) for objects of heterogeneous types. KeyValue answers clients' requests for an object provided its name and expected type. If the name is, indeed, mapped to an object of the given type, then the corresponding pointer is cast to this type and returned back to the client. Inheritance must be observed: When the client expects the object to be of a certain type, then KeyValue must fulfill the request if the object type derives from the given type. We need some type erasure mechanism that allows the compiler to treat pointers to objects of different types in a uniform way. Additionally, some type annotation must take place to allow for inheritance-conscious type checks against clients' expectations and, subsequently, safe type casts. Polymorphism is the obvious design to tackle this problem. All types derive from a common polymorphic base class. Type erasure here means that derived types are overlooked by the compiler, which sees only pointers-to-base. Type information is saved inside objects allowing for type checks and type casts through dynamic_cast. Unfortunately, for KeyValue, this wasn't an option because the types in question are defined by clients. KeyValue cannot assume and doesn't want to impose that all these types h