当前位置:网站首页>QT smart pointer error prone point

QT smart pointer error prone point

2022-07-25 23:48:00 StephenQin

  1. Qt The method of dynamic conversion between smart pointers in is qSharedPointerDynamicCast, Using this method does not increase the reference count .
  2. take this When the pointer is converted into a smart pointer , Can't use QSharedPointer(this) This way, , Instead, use sharedFromThis() This kind of method .
  3. Construction and type conversion are easy to make mistakes :
 Code of non smart pointer :
Base *p = new Derived();

 Error conversion writing of smart pointer :
// Wrong writing 1
QSharedPointer<Base> p(new Derived);
// Wrong writing 2
QSharedPointer<Base> p = QSharedPointer<Base>(new Derived);
// Write it correctly :
QSharedPointer<Base> p = QSharedPointer<Derived>(new Derived);
// Ensure that the types are consistent when constructing , The work of type conversion is left to ”=”.
原网站

版权声明
本文为[StephenQin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207252343471021.html