Qt:智能指针QScopedPointer 的使用(以及如何写一个QScopedPointer )

前言

本文讲述QScopedPointer 的使用,以及自己如何写一个QScopedPointer .

正文

QScopedPointer 的常用方法

以下是 QScopedPointer 的一些常用方法及其详细说明:

  1. 构造函数:

    • QScopedPointer<T> ptr(new T);
    • 用于创建一个 QScopedPointer,并管理一个新分配的对象。
  2. reset():

    • void reset(T *ptr = nullptr);
    • 释放当前管理的对象并重新指向新的对象。如果传入 nullptr,则仅释放当前管理的对象。
  3. operator->():

    • T *operator->() const;
    • 用于访问指针指向的对象的成员函数或变量。
  4. operator*():

    • T &operator*() const;
    • 用于访问指针指向的对象。
  5. data():

    • T *data() const;
    • 返回指针指向的对象的裸指针。
  6. T *get() const noexcept

    • 功能 :与 data() 功能相同,也返回当前 QScopedPointer 管理的对象的裸指针。
    • noexcept:表示该函数不会抛出异常。
    • 用法 :通常与 data() 等效,可以使用 get() 方法来访问裸指针。
  7. bool isNull() const noexcept

    • 功能 :检查 QScopedPointer 是否为空(即它是否指向一个有效的对象)。
    • noexcept:表示该函数不会抛出异常。
    • 用法:用于检查智能指针是否为空,常用于条件判断。
  8. T *take() noexcept

    • 功能 :释放 QScopedPointer 管理的对象并返回裸指针,智能指针不再管理该对象。
    • noexcept:表示该函数不会抛出异常。
    • 用法 :当你需要将对象的所有权转移到外部使用时,可以使用 take() 方法。调用 take() 后,智能指针不再管理该对象,因此外部代码需要负责删除它。
  9. void swap(QScopedPointer<T, Cleanup> &other) noexcept

    • 功能 :交换当前 QScopedPointer 和另一个 QScopedPointer 的管理对象。
    • noexcept:表示该函数不会抛出异常。
    • 用法 :用于交换两个 QScopedPointer 的对象,常用于实现自定义类型的 swap 操作或在算法中交换两个智能指针。

使用示例

cpp 复制代码
// TestSmartPointer.h
#ifndef TESTSMARTPOINTER_H
#define TESTSMARTPOINTER_H

#include <QObject>
#include <QScopedPointer>
#include <QSharedPointer>
class TestSmartPointer:public QObject
{
    Q_OBJECT
public:
    TestSmartPointer();
    ~TestSmartPointer();
    void doSomething();
};

#endif // TESTSMARTPOINTER_H

// TestSmartPointer.cpp
#include "testsmartpointer.h"
#include <QDebug>
TestSmartPointer::TestSmartPointer()
{
    qDebug()<<"对象创建";
}

TestSmartPointer::~TestSmartPointer()
{
    qDebug()<<"对象销毁";
}

void TestSmartPointer::doSomething()
{
    qDebug()<<"其它处理逻辑";
}


// main.cpp
#include <QCoreApplication>
#include "testsmartpointer.h"
#include "QDebug"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QScopedPointer<TestSmartPointer> smartPointer(new TestSmartPointer());
    smartPointer->doSomething();
    TestSmartPointer* tPointer = smartPointer.data();
    tPointer->doSomething();
    // 仅仅释放当前对象
    smartPointer.reset(nullptr);
    if (smartPointer.isNull()) {
        qDebug()<<"smartPointer is null";
    } else {
        qDebug()<<"smartPointer is not null";
    }


    QScopedPointer<TestSmartPointer> smartPointer2(new TestSmartPointer());
    if (smartPointer2.isNull()) {
        qDebug()<<"smartPointer2 is null";
    } else {
        qDebug()<<"smartPointer2 is not null";
    }
    // 智能指针不再管理该对象,需要自己管理
    TestSmartPointer* tPointer2 = smartPointer2.take();
    tPointer2->doSomething();
    delete tPointer2;

    return a.exec();
}
运行结果

实现类似 QScopedPointer 的智能指针

cpp 复制代码
template <typename T>
class ScopedPointerSelf
{
public:
    // 构造函数
    explicit ScopedPointerSelf(T *ptr = nullptr) : m_ptr(ptr) {}

    // 析构函数
    ~ScopedPointerSelf() {
        delete m_ptr;
    }

    // 禁止复制
    ScopedPointerSelf(const ScopedPointerSelf&) = delete;
    ScopedPointerSelf& operator=(const ScopedPointerSelf&) = delete;

    // 允许移动
    ScopedPointerSelf(ScopedPointerSelf&& other) noexcept : m_ptr(other.m_ptr) {
        other.m_ptr = nullptr;
    }

    ScopedPointerSelf& operator=(ScopedPointerSelf&& other) noexcept {
        if (this != &other) {
            delete m_ptr;
            m_ptr = other.m_ptr;
            other.m_ptr = nullptr;
        }
        return *this;
    }

    // 访问对象的方法
    T* operator->() const {
        return m_ptr;
    }

    // 解引用
    T& operator*() const {
        return *m_ptr;
    }
	bool operator!() const {
		return !m_ptr;
	}
	bool isNull() const {
		return !m_ptr;
	}
    // 重置指针
    void reset(T *ptr = nullptr) {
        delete m_ptr;
        m_ptr = ptr;
    }

    // 释放指针
    T* take() {
        T *temp = m_ptr;
        m_ptr = nullptr;
        return temp;
    }

    // 获取裸指针
    T* get() const {
        return m_ptr;
    }

private:
    T *m_ptr;
};

解释

这里只是实现了一个简单的智能指针的功能,肯定和源码中的没法比,不过该有的功能也都有。

  1. 构造函数和析构函数:构造函数初始化指针,析构函数自动删除管理的对象。
  2. 禁用复制:禁用复制构造函数和赋值操作符,以防止不必要的对象复制。
  3. 移动构造函数和移动赋值操作符:允许移动构造和移动赋值,以便在移动时转移资源所有权。
  4. 重置和释放 :提供 reset() 和 take() 方法来管理指针的生命周期和获取裸指针。

使用

cpp 复制代码
// 将我上述的内容放到testSmartPointer.h中,然后在main.cpp中
// 简单更改一下就能用了
#include <QCoreApplication>
#include "testsmartpointer.h"
#include "QDebug"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    ScopedPointerSelf<TestSmartPointer> smartPointer(new TestSmartPointer());
    smartPointer->doSomething();
    TestSmartPointer* tPointer = smartPointer.get();
    tPointer->doSomething();
    // 仅仅释放当前对象
    smartPointer.reset(nullptr);
    if (smartPointer.isNull()) {
        qDebug()<<"smartPointer is null";
    } else {
        qDebug()<<"smartPointer is not null";
    }


    ScopedPointerSelf<TestSmartPointer> smartPointer2(new TestSmartPointer());
    if (smartPointer2.isNull()) {
        qDebug()<<"smartPointer2 is null";
    } else {
        qDebug()<<"smartPointer2 is not null";
    }
    // 智能指针不再管理该对象,需要自己管理
    TestSmartPointer* tPointer2 = smartPointer2.take();
    tPointer2->doSomething();
    delete tPointer2;

    return a.exec();
}
运行结果

小结

请指教

相关推荐
§ꦿCFོ༉1 小时前
嵌入式八股文—C语言入门学习(四)
c语言·开发语言·学习
投图匠1 小时前
十种不同风格的 CTP接口 框架
开发语言·python
爱笑的k115 小时前
nano-vllm 源码剖析:千行 Python 手写离线推理引擎的调度、显存与并行时序
开发语言·python·vllm
X1A0RAN6 小时前
PPT 图片批量处理教程:导出全部幻灯片 · 提取全部素材
开发语言·javascript·powerpoint
熊猫钓鱼>_>7 小时前
普通用户视角的端侧智能体验手记【鸿蒙心迹】
开发语言·人工智能·华为·智能手机·性能优化·ai编程·harmonyos
瑞雪兆丰年兮7 小时前
JavaWeb-day13-SpringBootWeb事务管理&AOP
java·开发语言·springboot·javaweb·事务·web·aop
布莱克6059 小时前
Swift 常量详解:从 let 到编译期优化
服务器·开发语言·ssh·swift·常量·let
布莱克6059 小时前
Swift 可选类型(Optionals)详解:从原理到实战
开发语言·安全·ssh·swift·optional·可选类型
瑞雪兆丰年兮11 小时前
JavaWeb-day14-SpringBootWeb原理篇
java·开发语言·spring boot·spring·bean·mybatis·javaweb
SomeOtherTime13 小时前
Rust生命周期介绍(AI回答)
开发语言·后端·rust