0

Class story

I. Class basic

class, public:, private:: public members (create, start, stop)

can be called from outside the class. private members (_pipeline, _loop, _instance) are internal.

Constructor and destructor

1. Why is this constructor empty?

Detection() {}

https://www.geeksforgeeks.org/cpp/constructors-c/ https://www.geeksforgeeks.org/cpp/copy-constructor-in-cpp/

2. When does a constructor need content?

The rule: if every member initializes itself correctly, leave the constructor empty

Screenshot 2026-10-18 at 11.18.41.png

3. Why the destructor does have content

~Detection() { stop(); }

unique_ptr would free the pipeline automatically, but GStreamer needs these steps in this order:

  • quit the main loop
  • set the pipeline state to NULL
  • then free it

Member destructors only free memory; they don’t know about this order. That’s why the destructor needs code.

The rule: write a destructor when cleanup is more than freeing memory, or must happen in a specific order. If every member cleans up after itself, you don’t need a destructor at all.

After the destructor body runs, members are destroyed automatically in reverse order of declaration: _loop first, then _pipeline.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí