0

I am seeing a confusing bit of code in C++. What is happening with _capture_thread here. what is abc inheriting ?

class abc
{
   private:
           std::thread _capture_thread;
   public:
           abc();
           void start();

};

abc::abc() : _capture_thread()
{
   . 
   .
}

abc::start()
{
    _capture_thread = std::thread(param1, param2,...paramn);
}
badri
  • 575
  • 2
  • 8
  • 22
  • 1
    abc inherits nothing. The ` : _capture_thread()` is a [member initializer list](https://en.cppreference.com/w/cpp/language/constructor) – user4581301 Jul 16 '20 at 00:24
  • 1
    It's also redundant. You can drop `: _capture_thread()` part without changing the meaning of this code. – Igor Tandetnik Jul 16 '20 at 00:33
  • 1
    It would also be better to eliminate `abc::start()` entirely, and to initialise `_capture_thread` as needed in the constructor (or its initialiser list). Separating the two permits subsequent operations on `abc` to do things that rely on the thread being started, even if it has not been - which is a good recipe for undefined behaviour. It is trivial to delay the construction of an `abc` until it (or the thread it creates) actually needs to exist. – Peter Jul 16 '20 at 00:43
  • 1
    Note: If you initialize a thread in the initializer list, make damn sure it's the last member variable and nothing happens in the constructor's body. It really sucks if the thread starts and uses the object before it's ready. – user4581301 Jul 16 '20 at 15:44

0 Answers0