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

潍坊小企业网站建设人社局网站建设步骤

潍坊小企业网站建设,人社局网站建设步骤,餐厅网站模版,襄阳网站seoauto_ptr 和 unique_ptr 都是独占所有权的智能指针,但 unique_ptr 更加安全、灵活。 1. auto_ptr(C98产生) 特点 独占所有权:同一时间只能有一个 auto_ptr 管理对象。 转移语义:拷贝或赋值时会转移所有权&#xff08…

auto_ptr 和 unique_ptr 都是独占所有权的智能指针,但 unique_ptr 更加安全、灵活。


1. auto_ptr(C++98产生)

特点

  • 独占所有权:同一时间只能有一个 auto_ptr 管理对象。

  • 转移语义:拷贝或赋值时会转移所有权(原指针变为 nullptr)。

  • 不推荐使用:由于不安全的拷贝行为,C++11 起被废弃,C++17 移除。

底层实现(简化版)

cpp

template<typename T>
class auto_ptr {
private:T* ptr;public:explicit auto_ptr(T* p = nullptr) : ptr(p) {}~auto_ptr() { delete ptr; }// 拷贝构造函数(转移所有权)auto_ptr(auto_ptr& other) : ptr(other.release()) {}// 赋值操作(转移所有权)auto_ptr& operator=(auto_ptr& other) {if (this != &other) {delete ptr;       // 释放当前资源ptr = other.release(); // 接管 other 的资源}return *this;}// 释放所有权(返回原始指针,并置空)T* release() {T* temp = ptr;ptr = nullptr;return temp;}// 获取指针T* get() const { return ptr; }// 解引用T& operator*() const { return *ptr; }T* operator->() const { return ptr; }
};

问题

  1. 拷贝时会静默转移所有权

    cpp

    auto_ptr<int> p1(new int(42));
    auto_ptr<int> p2 = p1;  // p1 变成 nullptr,p2 接管资源1.容易导致意外的悬空智能指针。
  2. 不能用于 STL 容器(因为容器要求元素可拷贝,但 auto_ptr 的拷贝会改变原对象)。

  3. 不支持自定义删除器


2. unique_ptr(C++11 引入,推荐使用)

特点

  • 独占所有权(和 auto_ptr 一样)。

  • 禁止拷贝(但支持移动语义 std::move)。

  • 支持自定义删除器(可用于管理 FILE*malloc 内存等)。

  • 可用于 STL 容器(因为支持移动语义)。

底层实现(简化版)

cpp

template<typename T, typename Deleter = std::default_delete<T>>
class unique_ptr {
private:T* ptr;Deleter deleter;public:explicit unique_ptr(T* p = nullptr) : ptr(p) {}~unique_ptr() {if (ptr) deleter(ptr);}// 禁止拷贝unique_ptr(const unique_ptr&) = delete;unique_ptr& operator=(const unique_ptr&) = delete;// 支持移动构造unique_ptr(unique_ptr&& other) noexcept : ptr(other.release()), deleter(std::move(other.deleter)) {}// 支持移动赋值unique_ptr& operator=(unique_ptr&& other) noexcept {if (this != &other) {reset(other.release());deleter = std::move(other.deleter);}return *this;}// 释放所有权T* release() {T* temp = ptr;ptr = nullptr;return temp;}// 重置指针(先删除旧资源)void reset(T* p = nullptr) {if (ptr) deleter(ptr);ptr = p;}// 获取指针T* get() const { return ptr; }// 解引用T& operator*() const { return *ptr; }T* operator->() const { return ptr; }
};

优点

  1. 更安全

    • 禁止拷贝,避免 auto_ptr 的静默所有权转移问题。

    • 必须显式使用 std::move 转移所有权:

      cpp

      unique_ptr<int> p1(new int(42));
      unique_ptr<int> p2 = std::move(p1);  // p1 变为 nullptr
  2. 支持自定义删除器

    cpp

    auto file_deleter = [](FILE* f) { if (f) fclose(f); };
    unique_ptr<FILE, decltype(file_deleter)> file_ptr(fopen("test.txt", "r"), file_deleter);
  3. 可用于 STL 容器

    cpp

    vector<unique_ptr<int>> vec;
    vec.push_back(unique_ptr<int>(new int(10)));
http://www.dtcms.com/a/595867.html

相关文章:

  • 做网站如何推广买量手机网站栏目结构图
  • 建设电商网站的个人心得云南网站备案难吗
  • WordPress博客整站带数据做网站为什么要买服务器
  • 网站开发 项目的招标文件海口建设网站的公司哪家好
  • 网站主题和风格wordpress 图片相对路径
  • 二手车网站开发数据库设计网站 虚拟空间
  • 备案网站可以做接码平台么如何做实体店的网站
  • 已认证网站服务费怎么做房地产管理局网站
  • 网站建设工作流程html做外贸那个网站比较好
  • 成都发现6例阳性百度seo咋做
  • 深圳 公司网站设计滁州网站开发
  • 定海建设规划网站简述网络营销的特点及功能
  • 个体工商户网站备案设计 网站 源码
  • 镇江市网站建设网站推广技术
  • 设计类相关网站陆良建设局网站
  • 如何开网站呢红木家具网站建设总体规划
  • h5模版网站建一个网站怎么赚钱
  • 杭州做网站推广公司推荐公司注销 网站备案申请表
  • wordpress目录分页怎么弄东莞seo网站制作报价
  • 做网站要多少酷万网站建设
  • 广西南宁网站推广自豪得用wordpress删
  • 哈尔滨住房城乡建设局网站世界优秀网页设计赏析
  • 网站推广公司网站找做网站app
  • 六安网站制作金狮360免费建站怎么样
  • 网站首页制作案例现在从深圳回来需要隔离吗?
  • 海南省建设工程执业中心网站h5制作多少钱
  • 网站建设合同2018广州网站设计公司新闻
  • 网站开启微信支付功能网络营销推广怎么做
  • 公司建设网站的请示什么公司做的网站好
  • 广州网站建设招聘网站没有收录了