QScopedPointer
Scope:范围/作用域
明确两个概念:
1、智能指针不是指针
2、智能指针持有实际指针
作用
这个指针的作用如名字一般,在作用域内使用的指针.在变量超出作用域范围时自动调用指针类型的析构函数进行销毁.(实际就是利用栈的特性去管理堆上的内存)
构造函数
1 | template <typename T, typename Cleanup = QScopedPointerDeleter<T> > |
构造时传入T类型的指针p,并通过成员变量d进行保存(还是不由自主地感叹了下模板真好用^_^)
构造函数比较简单,按照其实现,用法如下:
1 | QScopedPointer<MyClass> mc(new MyClass); |
模板参数中的Cleanup默认使用的是QScopedPointerDeleter,即默认使用delete对指向的内存进行销毁。详见删除器小节。
如果仔细一些,会发现构造函数中有如下一行代码,并进行了实现:
1 |
|
这里其实是对隐式转换做了限制,避免用指针做比较时,除了转换成bool类型不能转换其他的类型,例如下面的代码会在编译期就提示你有错误:
1 | QScopedPointer<Foo> foo(nullptr); |
以下用法是支持的:
1 | QScopedPointer<Foo> foo(nullptr); |
析构函数
在QScopedPointer超出作用域后,智能指针本身(分配在栈上)会自动销毁,触发析构函数。在析构函数中,提供了统一的**清理**
函数Cleanup::cleanup(oldD),对oldD(智能指针指向的内存)进行销毁.
1 | inline ~QScopedPointer() |
关于Cleanup的介绍见下一小节.
删除器
QScopedPointer不仅适用于通过new操作符分配的单个内存和一组内存(数组),也可用于通过malloc进行分配的内存。同时,针对QObject的deleteLater特性,也有相关处理。这里要说一下他的Cleanup删除器(非专有名词,只有我这样称呼)的概念。
Cleanup用于超出作用域时自动销毁指针指向的内存,针对不同方式分配的内存,提供了不同的销毁方式:
删除器种类 | 如何销毁 | 销毁内存的分配方式 |
---|---|---|
QScopedPointerDeleter | delete | 使用new分配的内存 |
QScopedPointerArrayDeleter | delet[] | 使用new []分配内存 |
QScopedPointerPodDeleter | free | 内存由malloc进行分配 |
QScopedPointerObjectDeleteLater | delateLater | 当你的指针为QObject类型时,并且处于一个QEventLoop中 |
你也可以实现自己的删除器,它们需要具有公共静态函数 void cleanup(T *pointer).
1 | // this QScopedPointer deletes its data using the delete[] operator: |
方法一览
原型 | 接口说明 |
---|---|
QScopedPointer(T *p = …) | Constructs this QScopedPointer instance and sets its pointer to p. |
~QScopedPointer() | Destroys this QScopedPointer object. Delete the object its pointer points to. |
T * data() const | Returns the value of the pointer referenced by this object. QScopedPointer still owns the object pointed to. |
T * get() const | Same as data(). |
bool isNull() const | Returns true if this object is holding a pointer that is null. |
void reset(T *other = …) | Deletes the existing object it is pointing to (if any), and sets its pointer to other. QScopedPointer now owns other and will delete it in its destructor. To clear the pointer held without deleting the object it points to (and hence take ownership of the object), use take() instead. |
void swap(QScopedPointer<T, Cleanup> &other) | Swap this pointer with other. |
T * take() | Returns the value of the pointer referenced by this object. The pointer of this QScopedPointer object will be reset to null. Callers of this function take ownership of the pointer. |
bool operator bool() const | Returns true if this object is not null. This function is suitable for use in if-constructs, like:if (scopedPointer) {…} See also isNull(). |
bool operator!() const | Returns true if the pointer referenced by this object is null, otherwise returns false.See also isNull(). |
T & operator*() const | Provides access to the scoped pointer’s object.If the contained pointer is null, behavior is undefined.See also isNull(). |
T * operator->() const | Provides access to the scoped pointer’s object.If the contained pointer is null, behavior is undefined.See also isNull(). |
实际场景
1 | void myFunction(bool useSubClass) |
如果使用QScopedPointer,可以极大地较少代码量:
1 | void myFunction(bool useSubClass) |
QScopedPointer intentionally has no copy constructor or assignment operator, such that ownership and lifetime is clearly communicated.
QScopedPointer故意去除了拷贝构造和赋值构造函数,因此指针不能被传递和复制,这样关于指针的所有权和生命周期就很明确(跟随智能指针的销毁而销毁).
如果你的指针需要用const限定符去修饰,使用QScopedPointer后也是支持的
1 | const QWidget *const p = new QWidget(); |
QScopedArrayPointer
和QScopedPointer不同的是,他的删除器默认是QScopedPointerArrayDeleter,其他都差不多,我们了解即可
1 | template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> > |
源码赏析
1 | /**************************************************************************** |
附录
本文资料为以下链接的总结,可能大量借鉴其中内容,仅做分享之用,如有侵权,告知必删。