Why does this code not work?
std::shared_ptr<Event> e = ep->pop();
std::shared_ptr<TrackerEvent> t;
t = std::dynamic_pointer_cast<TrackerEvent>(e);
I get the following error:
/usr/include/c++/4.6/bits/shared_ptr.h:386: error: cannot dynamic_cast '(& __r)->std::shared_ptr<Event>::<anonymous>.std::__shared_ptr<_Tp, _Lp>::get [with _Tp = Event, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]()' (of type 'class Event*') to type 'class TrackerEvent*' (source type is not polymorphic)
TrackerEvent
inherits from Event
so I guess the problem is that I can't cast in this direction. But ep->pop()
might either return an object of type Event
or TrackerEvent
. And I was hoping that when I try to cast it to TrackerEvent
and it returns NULL
I would know whether I have an Event
or TrackerEvent
...
How would I do that?
Unlike other casts, a dynamic_cast involves a run-time type check. If the object bound to the pointer is not an object of the target type, it fails and the value is 0. If it's a reference type when it fails, then an exception of type bad_cast is thrown.
If dynamic_cast fails, it returns 0 . You may perform downcasts with the dynamic_cast operator only on polymorphic classes. In the above example, all the classes are polymorphic because class A has a virtual function. The dynamic_cast operator uses the runtime type information generated from polymorphic classes.
A pointer (or reference) to a class can actually point (refer) to any class derived from that class. Occasionally, it may be desirable to obtain a pointer to the fully derived class, or to some other subobject of the complete object. The dynamic cast provides this facility.
Dynamic Cast: A cast is an operator that converts data from one type to another type. In C++, dynamic casting is mainly used for safe downcasting at run time. To work on dynamic_cast there must be one virtual function in the base class.
The compiler is telling you what is going on at the end of the message:
(source type is not polymorphic)
Your Event
base class needs to have at least one virtual
member function (i.e. be a polymorphic type) in order to allow dynamic casts. You could make the destructor of Event
virtual:
class Event
{
public:
virtual ~Event() { /* whatever goes here, or nothing... */ }
// ...
};
Here is a live example with polymorphic types, showing that the code compiles (removing the virtual destructor would cause a compilation error similar to the one you are seeing).
As correctly mentioned by Luc Danton in the comments, a defaulted version of a virtual destructor can be defined this way (if your compiler is C++11-compliant in this respect):
class Event
{
public:
virtual ~Event() = default;
// ...
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With