Boost logo

Boost Users :

From: dariomt_at_[hidden]
Date: 2008-02-11 07:09:05


> Once its in the thread it seems to revert to class A's methods.

Slicing occurs here:

> thre.create_thread(*mea);
The B object is being copied as an A object.
I think it's when the temporary boost::function0<void> object passed
to create_thread() is constructed.

Try passing a refefence to A. e.g:

// warning: untested
template <typename T>
struct callable_ref
{
    T& m_ref;
    explicit callable_ref(T& ref) : m_ref(ref) {}
    void operator()() {m_ref();}
};

// no copy -> no slicing
// reference to A -> virtual B::operator() is called
thre.create_thread( callable_ref<A>(*mea) );

By the way, I think you can't use boost::ref, as
boost::reference_wrapper does not have operator()
thre.create_thread( boost::ref(*mea) ); // doesn't work?

Maybe TR1 can do it as std::tr1::reference_wrapper should have an
appropriate operator()
thre.create_thread( std::tr1::ref(*mea) ); // probably works



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