• c++ 智能指针 (std::auto_ptr)


    定义于头文件 

    template< class T > class auto_ptr;

    (1)(C++11 中弃用)
    (C++17 中移除)

    template<> class auto_ptr;

    (2)(C++11 中弃用)
    (C++17 中移除)

    auto_ptr 是通过由 new 表达式获得的对象,并在 auto_ptr 自身被销毁时删除该对象的智能指针。它可用于为动态分配的对象提供异常安全、传递动态分配对象的所有权给函数和从函数返回动态分配的对象。

    复制 auto_ptr ,会复制指针并转移所有权给目标: auto_ptr 的复制构造和复制赋值都会修改其右侧参数,而且“副本”不等于原值。因为这些不常见的复制语义,不可将 auto_ptr 置于标准容器中。此用途及其他使用更适合用 std::unique_ptr 。 (C++11 起)

    2) 提供对类型 void 的特化,它声明 typedef element_type ,但无成员函数。

    文档中到处使用附加的类模板 auto_ptr_ref 。它是保有到 auto_ptr 引用的实现定义类型。允许实现提供拥有不同名称的模板,或以其他方式实现返回它或接受它为参数的函数。

    构造函数

    std::auto_ptr<T>::auto_ptr

    explicit auto_ptr( X* p = 0 ) throw();

    (1)(C++11 中弃用)
    (C++17 中移除)

    auto_ptr( auto_ptr& r ) throw();

    (2)(C++11 中弃用)
    (C++17 中移除)

    template< class Y >
    auto_ptr( auto_ptr& r ) throw();

    (3)(C++11 中弃用)
    (C++17 中移除)

    auto_ptr( auto_ptr_ref m ) throw();

    (4)(C++11 中弃用)
    (C++17 中移除)

    从指代待管理对象的指针构造 auto_ptr

    1) 以指针 p 构造 auto_ptr

    2) 以 r 所保有的指针构造 auto_ptr 。调用 r.release() 获取该对象的所有权。

    3) 同 (2) 。 Y* 必须可隐式转换为 T* 。

    4) 以 m 所指代的 auto_ptr 实例中保有的指针构造 auto_ptr 。对 m 所保有的 auto_ptr p 调用 p.release() 以获取该对象的所有权。

    auto_ptr_ref 是保有到 auto_ptr 引用的实现定义类型。 std::auto_ptr 可隐式转换成此类型并可从它赋值。允许实现以不同的名称提供该模板,或以其他方式实现等价的功能。

    参数

    p-指向待管理对象的指针
    r-另一 auto_ptr ,从它传递所有权
    m-保有到 auto_ptr 的引用的实现定义类型对象

    注意

    源自 auto_ptr_ref 的构造函数和复制赋值运算符允许从无名临时量复制构造和赋值 std::auto_ptr 。因为其复制构造函数与复制赋值运算符以非 const 引用接收参数,它们不能直接绑定右值。然而能执行用户定义转换(释放原 auto_ptr ),之后再调用以值接收 auto_ptr_ref 的构造函数或复制赋值运算符。这是移动语义的早期实现。

     

    析构函数

    std::auto_ptr<T>::~auto_ptr

    ~auto_ptr() throw();

    (C++11 中弃用)
    (C++17 中移除)

    销毁管理的对象。调用 delete get() 。

    从另一 auto_ptr 转移所有权

    std::auto_ptr<T>::operator=

    auto_ptr& operator=( auto_ptr& r ) throw();

    (1)(C++11 中弃用)
    (C++17 中移除)

    template< class Y >
    auto_ptr& operator=( auto_ptr& r ) throw();

    (2)(C++11 中弃用)
    (C++17 中移除)

    auto_ptr& operator=( auto_ptr_ref m ) throw();

    (3)(C++11 中弃用)
    (C++17 中移除)

    r 所管理的对象替换被管理对象。

    1) 等效于调用 reset(r.release()) 。

    2) 等效于调用 reset(r.release()) 。 Y* 必须可隐式转换为 T* 。

    3) 等效于调用 reset(m.release()) 。 auto_ptr_ref 是保有到 auto_ptr 引用的实现定义类型。 std::auto_ptr 可隐式转换到及转换自此类型。允许实现以不同的名称提供该模板,或以其他方式实现等价的功能。

    参数

    r-另一 auto_ptr ,从它转移对象的所有权
    m-保有到 auto_ptr 引用的实现定义类型对象

    返回值

    *this 。

    转换被管理指针到指向另一类型的指针

    std::auto_ptr<T>::operator auto_ptr<Y>

    template< class Y >
    operator auto_ptr_ref() throw();

    (1)(C++11 中弃用)
    (C++17 中移除)

    template< class Y >
    operator auto_ptr() throw();

    (2)(C++11 中弃用)
    (C++17 中移除)

    转换 *this 为对于相异类型 Yauto_ptr

    1) 返回保有到 *this 引用的实现定义类型对象。 std::auto_ptr 可转换并且可赋值自此模板。允许实现以不同的名称提供该模板,或以其他方式实现等价的功能。

    2) 以调用 release() 获得的指针构造新的 auto_ptr

    参数

    (无)

    返回值

    1) 保有到 *this 的引用的实现定义类型对象。

    2) 拥有由调用 release() 获得的指针的 auto_ptr

    返回指向被管理对象的指针

    std::auto_ptr<T>::get

    T* get() const throw();

    (C++11 中弃用)
    (C++17 中移除)

    返回 *this 所保有的指针。

    参数

    (无)

    返回值

    *this 所保有的指针。

    访问被管理对象

    1. std::auto_ptr<T>::operator*,
    2. std::auto_ptr<T>::operator->

    T& operator*() const throw();

    (1)(C++11 中弃用)
    (C++17 中移除)

    T* operator->() const throw();

    (2)(C++11 中弃用)
    (C++17 中移除)

    解引用指向被管理对象的指针。第一版本要求 get() != 0 。

    参数

    (无)

    返回值

    1) *get() 。

    2) get() 。

    替换被管理对象

    std::auto_ptr<T>::reset

    void reset( T* p = 0 ) throw();

    (C++11 中弃用)
    (C++17 中移除)

    替换 p 为保有的指针。若当前实现保有的指针不是空指针,则调用 delete get() 。

    参数

    p-指向待管理对象的指针

    返回值

    (无)

    释放被管理对象的所有权

    std::auto_ptr<T>::release

    T* release() throw();

    (C++11 中弃用)
    (C++17 中移除)

    返回保有的指针。调用后 *this 保有空指针。

    参数

    (无)

    返回值

    get() 。

     

  • 相关阅读:
    数据结构速通-重点知识点回顾
    【Linux】Ubuntu 部署 Zabbix 7.0
    Apereo CAS反序列化漏洞中数据加解密研究
    Doris 数据分布—Partition
    数据结构-图-最短路径问题
    Android学习笔记 7. ToolBar
    C++语法——右值引用、移动构造和赋值、万能引用和转发、move和forward底层实现
    3.使用IDE的优点
    GaussDB_T 单机版轻量安装
    [Leetcode]138. 复制带随机指针的链表
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/126795811