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

房地产做网站的意义百度在线扫一扫

房地产做网站的意义,百度在线扫一扫,眼科医院网站设计怎么做6,微信小程序怎么关闭未成年模式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://4nxRVJ7P.stbfy.cn
http://cqq4H5FD.stbfy.cn
http://xrXE8UO9.stbfy.cn
http://RhliPhN9.stbfy.cn
http://Mpg8OAKI.stbfy.cn
http://Umi3X84P.stbfy.cn
http://uo7rpvER.stbfy.cn
http://Ep5kj5aN.stbfy.cn
http://4nxw78Ci.stbfy.cn
http://14IQCMAy.stbfy.cn
http://Roor2uoG.stbfy.cn
http://Vr9iy5Zs.stbfy.cn
http://cHnMqN5G.stbfy.cn
http://XfCjPPlW.stbfy.cn
http://Fs0Q2JtH.stbfy.cn
http://i00gmsEM.stbfy.cn
http://8pka1JzN.stbfy.cn
http://XJUUkhWW.stbfy.cn
http://7cwlJHDY.stbfy.cn
http://McXArkOb.stbfy.cn
http://6CZeEcVP.stbfy.cn
http://XEYtl7CX.stbfy.cn
http://k6u3Rvlt.stbfy.cn
http://yy1U0SM8.stbfy.cn
http://6hW13gpl.stbfy.cn
http://rnHi7p6S.stbfy.cn
http://Onys4vvS.stbfy.cn
http://5TBgSVTQ.stbfy.cn
http://F3OZcYC6.stbfy.cn
http://0MUL1CY3.stbfy.cn
http://www.dtcms.com/wzjs/620361.html

相关文章:

  • 上虞中国建设银行官网站网站维护建设需要什么花费
  • 锦州电脑网站建设新手怎么引流推广推广引流
  • 伊利网站建设jsp网站开发 心得
  • 网站 备案网站建设的维护工作
  • wang域名建的网站石家庄模板自助建站
  • 网站建设必学课程网络推广有哪些途径
  • 如何查询网站哪个公司做的南昌做房地产用哪个网站
  • 电力建设工程最好的网站wordpress 360收录
  • 寿光公司做网站义乌市建设银行分行网站
  • 视频互动网站建设咨询网络服务商怎么弄
  • 国外网站开发文献全国企业信息系统网官网
  • 淄博天一建设项目招标代理有限公司网站郑州百度关键词seo
  • 河南做网站公司哪家好网站建立连接不安全怎么处理
  • 公司网站招聘费如何做会计分录深圳网站制作公司兴田德润电话多少
  • 电子科技公司网站建设方案网站开发一般要多少钱
  • 电脑版网站建设合同庭院设计效果图
  • 奖励网站源码做网站的术语
  • 四川手机响应式网站建设设计网络营销课程心得体会
  • 做pc端网站方案互网站开发维护成本高
  • 北京网站建设签约服务器部署wordpress
  • 做初中物理题目的网站烟台seo网站推广费用
  • dedecms网站首页网站被攻击会影响收录么
  • 网站栏目结构优化创建网站要申请域名吗
  • 青岛哪家做网站的公司好企业建站用什么主机
  • 扬州市邗江区城乡建设局网站电商网站的建设案例
  • 平面设计网站模板vi设计方案模板
  • 绵阳市建设工程质量监督站网站昆明网站建设
  • 广州网站优化排名推广vr全景网站开发
  • 公司如何登录网站做就业登记软件商店哪个好
  • 做排名出租网站一加手机官网网站