Possible Duplicate:
Start thread with member function
I'm VERY new to C++. My experience has mostly been with javascript and java.
I'm using Xcode on Lion. The following code gives me a compilation error "Reference to non-static member function must be called; did you mean to call it with no arguments?"
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread(handler);
}
};
I also tried this->handler
, &handler
, and other variations, but none of them worked. This code compiles though and accomplishes what I want it to:
class MyClass {
private:
void handler() {
}
public:
void handleThings() {
std::thread myThread([this]() {
handler();
});
}
};
Why can't I pass a reference to a member function? Is my work-around the best solution?
std::thread myThread(&MyClass::handler, this);
myThread.join();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With