C++ 異常庫 - bad_weak_ptr



描述

這是一個無效的弱指標。

宣告

以下是 std::bad_weak_ptr 的宣告。

class bad_weak_ptr: public exception;

C++11

class bad_weak_ptr: public exception;

引數

返回值

異常

無異常保證 - 無成員丟擲異常。

示例

以下為 std::bad_typeid 的示例。

#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';
   }
}

示例輸出如下:

bad_weak_ptr
exception.htm
廣告
© . All rights reserved.