C++ using this pointer in constructors

后端 未结 8 993
时光取名叫无心
时光取名叫无心 2020-12-15 16:45

In C++, during a class constructor, I started a new thread with this pointer as a parameter which will be used in the thread extensively (say, call

8条回答
  •  有刺的猬
    2020-12-15 17:42

    Depends on what you do after starting the thread. If you perform initialization work after the thread has started, then it could use data that is not properly initialized.

    You can reduce the risks by using a factory method that first creates an object, then starts the thread.

    But I think the greatest flaw in the design is that, for me at least, a constructor that does more than "construction" seems quite confusing.

提交回复
热议问题