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

网站建设 风险说明/下载百度

网站建设 风险说明,下载百度,盗墓笔记wordpress小说主题,网站开发做原型吗auto_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.whsansanxincailiao.cn/news/30338598.html

相关文章:

  • 龙华做网站怎么样/seo百科
  • 网站制作中文版/房产网站建设
  • 雅布设计创始人/想找搜索引擎优化
  • 一家专做中式设计的网站/百度商家平台客服电话
  • 织梦网站后台密码忘记了怎么做/成都培训机构排名前十
  • aspx网站模板/下载百度地图2022最新版
  • wordpress主题不显示图片/宁波seo推广优化哪家强
  • 做网站用百度地图和天地图/抖音黑科技引流推广神器
  • 太原网站设计公司/网络推广网站建设
  • vue做的网站模板/东莞seo整站优化
  • 能够做冶金工程毕业设计的网站/windows优化大师使用方法
  • 做电商网站一般多少钱/营销手段和技巧
  • 义乌免费做网站/百度快速排名优化工具
  • 商业空间设计ppt/宁波优化seo软件公司
  • 龙岗营销网站建设公司/磁力搜索器 磁力猫
  • 硅藻泥网站怎么做/阿里数据
  • 贵阳公司做网站/磁力链接搜索引擎2021
  • 做网站遇到各种问题/现在做网络推广都有什么方式
  • 晋江网站开发/广州市人民政府新闻办公室
  • 网站销售怎么做/网络营销建议
  • 科技时代/百度seo技术优化
  • 长图可以在哪些网站做/上海职业技能培训机构一览表
  • 昆明网站推广8888168/线上宣传方式有哪些
  • erp系统有什么用/公司seo排名优化
  • 博物馆门户网站建设优势/长春网站建设设计
  • wordpress gallery 尺寸/无锡百度关键词优化
  • 咸宁公司做网站/温州seo服务
  • wordpress访问目录权限/搜索引擎seo优化
  • 网站运营面试问题/百度投放广告平台
  • 辽宁营商建设局网站/百度付费推广有几种方式