C++ typeinfo 庫 - bad_typeid



描述

它在空指標的 typeid 上丟擲異常。

宣告

以下是 std::bad_typeid 的宣告。

C++98

	
class bad_typeid;

C++11

class bad_typeid;

引數

返回值

異常

不丟擲異常保證 - 此成員函式從不丟擲異常。

資料競爭

區域設定物件被修改。

示例

以下為 std::bad_typeid 的示例。

#include <iostream>
#include <typeinfo>
 
struct S {
   virtual void f();
};

int main() {
   S* p = nullptr;
   try {
      std::cout << typeid(*p).name() << '\n';
   } catch(const std::bad_typeid& e) {
      std::cout << e.what() << '\n';
   }
}

輸出應如下所示:

Attempted a typeid of NULL pointer!
typeinfo.htm
廣告
© . All rights reserved.