当前位置: 首页 > article >正文

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. T *data()

    • 返回指针指向的对象的裸指针。
  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 操作或在算法中交换两个智能指针。

使用示例

// 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 的智能指针

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() 方法来管理指针的生命周期和获取裸指针。

使用

// 将我上述的内容放到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();
}

运行结果

在这里插入图片描述

小结

请指教


http://www.kler.cn/news/314251.html

相关文章:

  • [SDX35+WCN6856]SDX35 开启class/gpio子系统配置操作说明
  • 在Python中,类是用于定义对象的蓝图或模板,而对象则是根据类创建的具体实例
  • 【RPA私教课:UIPath】RPA 赋能科技企业,登录时验证码自动截取
  • SpringBoot 消息队列RabbitMQ 消息可靠性 数据持久化 与 LazyQueue
  • pytorch 同步机制
  • docker desktop windows stop
  • @EnableScheduling 和 @Scheduled 实现定时任务的任务延期问题
  • Linux:login shell和non-login shell以及其配置文件
  • MQ入门(4)
  • kubernetes基础命令
  • 论文阅读--Planning-oriented Autonomous Driving(一)
  • “华为杯”第十二届中国研究生数学建模竞赛-D题:面向节能的单/多列车优化决策问题研究(续)(附MATLAB代码实现)
  • IPsec-VPN中文解释
  • 数据结构:二叉树(一)
  • 【计算机网络】详解UDP套接字网络字节序IP地址端口号
  • 2025年最新大数据毕业设计选题-Hadoop综合项目
  • TCP客户端编码和解码处理:发送和接收指定编码消息
  • 深度学习——基础知识
  • 初识zookeeper
  • 计组(蒋)期末速成笔记1
  • 基于MATLAB的运动模糊图像处理
  • LVGL学习
  • ES6 -- 2015
  • 肺结节检测系统源码分享
  • CLion/Git版本控制
  • 使用Rust直接编译单个的Solidity合约
  • VCG 顶点区域生长
  • 成都睿明智科技有限公司电商服务引领品牌跃升
  • 群晖NAS使用Docker本地部署网页版Ubuntu系统并实现无公网IP远程访问
  • 人工智能之就业方向(The Employment Direction of Artificial Intelligence)