Skip to main content
gamedev.net
🔒 Locked

boost::thread(*this)

Started by bcome Dec 3, 2004 at 12:23 AM 0 replies 850+ views
Original Post
bcome
bcome
I'm trying to compile something like this:

class ThreadedClass
{
public:
    boost::thread *this_thread;

    ThreadedClass();
    ~ThreadedClass();

    void operator ()();
}
ThreadedClass::ThreadedClass()
{
    this_thread = new boost::thread(*this);
}
ThreadedClass::~ThreadedClass()
{
    if (this_thread) delete this_thread;
}
void ThreadedClass::operator ()()
{
    std::cout << "void ThreadedClass::operator ()()!";
    std::cout << std::endl;
}


I have this simplified class and the real class in the same project. The simplified class compiles just fine, yet the real one gives me:

c:\some\folder\someproject\somesourcefile.cpp(36) : error C2664: 'boost::thread::thread(const boost::function0<R> &)' :
cannot convert parameter 1 from 'Namespace::RealClass' to 'const boost::function0<R> &'
        with
        [
            R=void
        ]
        and
        [
            R=void
        ]
        Reason: cannot convert from 'Namespace::RealClass' to 'const boost::function0<R>'
        with
        [
            R=void
        ]
        No constructor could take the source type, or constructor overload resolution was ambiguous
What is wrong? Also I can't even do this:

typedef void (*ThreadFunction)();
ThreadFunction = *this;
I'm compiling under MSVC++ .net Toolkit 2003 with MSVC++ .net 2003 SE headers & libraries. Edit: line split for ease.
<!-- ................................ -->u r what u bcomeMy (XHTML
joanusdmentia
joanusdmentia
You'll want to use boost::bind():
this_thread = new boost::thread(boost::bind(MyThreadFunc,this));


Quote:
typedef void (*ThreadFunction)();

ThreadFunction = *this;

Of course it won't work, this points to an instance of ThreadedClass, *not* a function. operator() doesn't make the class a function, operator() is just a normal function. Passing a class with operator() like a function will only work if the parameter is templated, ie
template<typename Func>void dostuff(Func func){    func();}struct MyFunc{    void operator()()    {        // do stuff    }};void test(){    MyFunc myfunc;    dostuff(myfunc);}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.