std::bad_weak_ptr

定义于头文件 <memory>
class bad_weak_ptr;
(C++11 起)

std::bad_weak_ptrstd::shared_ptrstd::weak_ptr 为参数的构造函数,在 std::weak_ptr 指代已被删除的对象时,作为异常抛出的对象类型。

cpp/error/exceptionstd-bad weak ptr-inheritance.svg
关于这幅图像

继承图

目录

成员函数

(构造函数)
构造 bad_weak_ptr 对象
(公开成员函数)

std::bad_weak_ptr::bad_weak_ptr

bad_weak_ptr() noexcept;

构造 std::bad_weak_ptr 的新实例。 what() 返回实现定义的空终止字节字符串。

参数

(无)

继承自 std::exception

成员函数

析构该异常对象
(std::exception 的虚公开成员函数)
[虚]
返回解释性字符串
(std::exception 的虚公开成员函数)

示例

#include <memory>
#include <iostream>
int main()
{
    std::shared_ptr<int> p1(new int(42));
    std::weak_ptr<int> wp(p1);
    p1.reset();
    try {
        std::shared_ptr<int> p2(wp);
    } catch(const std::bad_weak_ptr& e) {
        std::cout << e.what() << '\n';
    }
}

输出:

std::bad_weak_ptr

参阅

(C++11)
拥有共享对象所有权语义的智能指针
(类模板)
(C++11)
std::shared_ptr所管理的对象的弱引用
(类模板)

版本历史

  • (当前 | 先前 2017年9月25日 (一) 21:49Fruderica讨论 | 贡献 . . (1,327字节) (-3). . (format) (撤销)
  • 当前 | 先前 2017年8月26日 (六) 21:10Fruderica讨论 | 贡献. . (1,330字节) (-250). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:55P12bot讨论 | 贡献 . . (1,580字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:02P12bot讨论 | 贡献 . . (1,580字节) (-93). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:39P12bot讨论 | 贡献 . . (1,673字节) (+249). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:25P12讨论 | 贡献 . . (1,424字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,424字节) (-28). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:09P12讨论 | 贡献 . . (1,452字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,452字节) (+1,452). . (Translated from the English version using Google Translate)