首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::bad_weak_ptr

Defined in header <memory>

class bad_weak_ptr;

(since C++11)

std::bad_weak_ptr的构造函数作为异常抛出的对象的类型。std::shared_ptr那就std::weak_ptr作为论据,当std::weak_ptr引用已删除的对象。

二次

二次

继承图

成员函数

(constructor)

constructs the bad_weak_ptr object (public member function)

继承自STD:例外

成员函数

(destructor) virtual

destructs the exception object (virtual public member function of std::exception)

what virtual

returns an explanatory string (virtual public member function of std::exception)

二次

代码语言:javascript
复制
#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';
    }
}

二次

产出:

二次

代码语言:javascript
复制
std::bad_weak_ptr

二次

另见

shared_ptr (C++11)

smart pointer with shared object ownership semantics (class template)

weak_ptr (C++11)

weak reference to an object managed by std::shared_ptr (class template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券