C++ Qt 中的智能指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1481616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:08:43  来源:igfitidea点击:

Smart pointers in Qt

c++qtsmart-pointers

提问by alexkr

Like it has been written hereQt up to now has 8 specilized smart pointer classes. It looks like it is all you will ever need. However, in order to use any of these smart pointers your class must be derived from QObject which is not always convenient. Is there other implementations of smart pointers in Qt which work with arbitrary classes?

就像这里写的那样Qt 到现在为止有 8 个专门的智能指针类。看起来这就是您所需要的一切。但是,为了使用这些智能指针中的任何一个,您的类必须从 QObject 派生,这并不总是很方便。Qt 中是否有其他智能指针实现可以与任意类一起使用?

采纳答案by Paul Dixon

Many Qt classes are derived from QObject, and while some of the built in smart pointer classes are related to QObject (or QSharedData), the QSharedPointerand QScopedPointertemplates appear to allow pointers to anything.

许多 Qt 类是从 QObject 派生的,虽然一些内置的智能指针类与 QObject(或 QSharedData)相关,但QSharedPointerQScopedPointer模板似乎允许指向任何东西的指针。

Beyond that, you'll find some smart pointer templatesin Boost:

除此之外,您会在Boost 中找到一些智能指针模板

  • scoped_ptr- Simple sole ownership of single objects. Noncopyable.
  • scoped_array- Simple sole ownership of arrays. Noncopyable.
  • shared_ptr- Object ownership shared among multiple pointers.
  • shared_array- Array ownership shared among multiple pointers.
  • weak_ptr- Non-owning observers of an object owned by shared_ptr.
  • intrusive_ptr- Shared ownership of objects with an embedded reference count.
  • scoped_ptr- 单个对象的简单唯一所有权。不可复制。
  • scoped_array- 数组的简单唯一所有权。不可复制。
  • shared_ptr- 在多个指针之间共享的对象所有权。
  • shared_array- 在多个指针之间共享的数组所有权。
  • weak_ptr- shared_ptr 拥有的对象的非拥有观察者。
  • intrusive_ptr- 具有嵌入引用计数的对象的共享所有权。