Smart indicators and follow-up from QObject



  • One man told me that for QObject class, it makes no sense to use smart indexes. (I had windows in my classroom like std:unique_ptr). For example:

    std::unique_ptr<Commands> commands; // объявление в заголовочнике
    

    commands = std::make_unique<Commands>(); // создание в cpp

    In fact, it was said that, "for the QObject class, it doesn't make sense to use smart indexes" and you need to be replaced by normal.
    Want to figure out why? How is that a judgement? If you can, some source can read this.



  • It is likely that Qt adopts the concept of parenthood (see). https://doc.qt.io/qt-5/qobject.html#QObject who is responsible for the possession of a subsidiary object. That is, when the parent is destroyed, its subsidiary objects are also destroyed. If you put this concept into possession std::unique_ptrwe'll get the possibility of double removal.

    It is clear that in cases where QObject created dynamically without a parent (parent==nullptrand must be destroyed somehow, std::unique_ptr has the right to exist. But you can also use (but I don't think it's advisable) and the Qt-special wraps like QScopedPointer

    I mean. QObject + std::unique_ptr It's a good couple, but provided the object doesn't own (or not own) another one. QObject♪ Otherwise, at least it would look like the use of belts and lifts at the same time, and at a maximum it would lead to memory problems during performance.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2