2

Is it legal to use this pointer in constructor initialization list in following case? I'm trying to start some background thread with task which is class method. Is it a good approach at all, can I do better? It works with clang (LLVM 3.5), but I'm afraid of undefined behavior.

class SomeClass  {
    public:
        SomeClass();

    private:
        std::thread syncThread;
        void syncTask();
};


SomeClass::SomeClass()
    :syncThread(&SomeClass::syncTask, this) { // is it legal? object is under construction
}
vard
  • 2,142
  • 4
  • 30
  • 40

0 Answers0