2

I know the question will be marked as duplicate, because I , too, have read couple of similar questions on SO. But unfortunately none of the answers worked for me. I tried all of 'em and as a last option I wanted to ask.

void AsyncClass::method1()
{
    cout << "method is called" << endl;
}

void AsyncClass::method2()
{
    auto t = new std::thread(this->method1);
}

Both of the methods are public and non-static. This doesn't compile saying

non-standard syntax; use '&' to create a pointer to member

Also considering the answers on SO I have tried

auto t = new std::thread(this->method1);
auto t = new std::thread(this->*method1);
auto t = new std::thread(&(this->method1));
auto t = new std::thread(&AsyncClass::method1);

None of them compiled. What is the correct way to to it?

ozgur
  • 2,549
  • 4
  • 25
  • 40

1 Answers1

2

You should do:

auto t = new std::thread(&AsyncClass::method1, this);
ISanych
  • 21,590
  • 4
  • 32
  • 52
  • It worked. But why? what if **method1** takes parameters? Will **this** be treated as parameter? – ozgur Nov 17 '15 at 17:19
  • `this` essentially _is_ the hidden, first parameter to (non-static) member functions. – TripeHound Nov 17 '15 at 17:21
  • all non static methods have implicit parameter this (so they know which instance they operate with), all other parameters go after this – ISanych Nov 17 '15 at 17:21
  • Yes I have tried and it works without problem. I need to wait for 4 minutes in order to accept the answer. – ozgur Nov 17 '15 at 17:22