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?
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;
// ...
};