SFML community forums
Help => System => Topic started by: Pyrius on October 15, 2011, 09:58:17 pm
-
What syntax would I use to run a class function as a Thread with an object and a parameter? For example:
Thread thread(foo.bar(param));
thread.Launch();
Thanks in advance.
-
You can't do that directly, but you can use std::bind() or boost::bind():
class Foo { void bar(int x); };
Foo foo;
Thread thread( std::bind(&Foo::bar, &foo, 7) );
See the Boost.Bind documentation (http://www.boost.org/doc/libs/1_47_0/libs/bind/bind.html). The alternative is a self-written functor with operator() or a lambda expression in C++11.
-
What do I #include to use std::bind?
-
The <functional> header. Your standard library must support C++11, though (VS 2010 or a newer g++ with the flag -std=c++0x). Otherwise, use the Boost or TR1 version.