Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-06-25 07:32:02


My idea is based on fact that you (program developer or another developer) know what is going to
be send. What you are trying to do is erasing the type to some base and that would not work.

So you send event must handle derived type and put it correctly to variant. A possible solution
would be:

template<class EventType_>
void sendEvent(EventType_ const& ev)
{
   m_eventContainer = ev; //<- now compiler knows exactly the type of ev
m_derivedHelper->dispatchEvent(&m_eventContainer);
}

Best Regards,
Ovanes

On Fri, June 22, 2007 21:41, Lukasz wrote:
>
> Thanks for the fast reply. Your solution looked very good for my problem, so i tried to apply it.
>However, there seems to be a problem. At the point when i call dispatchEvent (as in the below
>example) is when i have the actual event to be sent. But i still only have a pointer to the base
>class and by assigning it to the container, it always gets interpreted as a base event, even when
>in reality it's some derived type.
>
> I tried a couple of approaches. In the first one, my variant contains only the derived type
(base class is Event, derived is EventTest) , i.e.
>
> typedef boost::variant<EventTest> event_container;
>
> ....
> event_container m_eventContainer;
> ...
>
> void sendEvent(Event * event)
> {
> // Call Helper to dispatch the event to correct type
> m_eventContainer = *event;
> m_derivedHelper->dispatchEvent(&m_eventContainer);
> }
>
> This won't work. The compiler complains that none of the overloads for
>boost::detail::variant::make_initializer_node::apply can convert parameter 2. If i add the base
>event type to the variant list, i.e.
>
> typedef boost::variant<Event, EventTest> event_container;
>
> it will work, but it will always trigger the processEvent with the base
> class type. I haven't found any information as to how boost::variant treats a case of a pointer to
>a base class (in this case, i'm not even actually passing the pointer but an object since for
>pointers it just doesn't seem to work). Should it be able to automatically convert the type ? If
>not, then i guess i'm back to square one, since i still can't cast the event... unless i maybe
>misunderstood something ? The code i tried is pretty much what you gave me, with some minor
>modifications.
>
> Thanks in advance for any further assistance.
>
> Lukasz K.
>
>
[...]


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net