Namespaces
Variants

std:: bad_typeid

From cppreference.net
Utilities library
헤더 파일에 정의됨 <typeinfo>
class bad_typeid : public std:: exception

이 유형의 예외는 다형성 유형의 역참조된 널 포인터 값에 typeid 연산자가 적용될 때 발생합니다.

cpp/error/exception std-bad typeid-inheritance.svg

상속 다이어그램

목차

멤버 함수

(생성자)
새로운 bad_typeid 객체를 생성함
(public 멤버 함수)
operator=
bad_typeid 객체를 대체함
(public 멤버 함수)
what
설명 문자열을 반환함
(public 멤버 함수)

std::bad_typeid:: bad_typeid

(1)
bad_typeid ( ) throw ( ) ;
(C++11 이전)
bad_typeid ( ) noexcept ;
(C++11 이후)
(C++26 이후 constexpr)
(2)
bad_typeid ( const bad_typeid & other ) throw ( ) ;
(C++11 이전)
bad_typeid ( const bad_typeid & other ) noexcept ;
(C++11 이후)
(C++26 이후 constexpr)

새로운 bad_typeid 객체를 생성합니다. 구현 정의 널 종료 바이트 문자열을 포함하며, 이는 what() 을 통해 접근할 수 있습니다.

1) 기본 생성자.
2) 복사 생성자. * this other 가 모두 std::bad_typeid 동적 타입을 가질 경우 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. (C++11 이후)

매개변수

other - 복사할 다른 예외 객체

std::bad_typeid:: operator=

bad_typeid & operator = ( const bad_typeid & other ) throw ( ) ;
(C++11 이전)
bad_typeid & operator = ( const bad_typeid & other ) noexcept ;
(C++11 이후)
(C++26 이후 constexpr)

other 의 내용을 할당합니다. 만약 * this other 모두 동적 타입 std::bad_typeid 를 가진다면, 할당 후 std:: strcmp ( what ( ) , other. what ( ) ) == 0 입니다. (C++11 이후)

매개변수

other - 할당할 다른 예외 객체

반환값

* this

std::bad_typeid:: what

virtual const char * what ( ) const throw ( ) ;
(C++11 이전)
virtual const char * what ( ) const noexcept ;
(C++11 이후)
(C++26 이후 constexpr)

설명 문자열을 반환합니다.

반환값

설명 정보를 포함하는 구현 정의 널 종료 문자열에 대한 포인터입니다. 이 문자열은 std::wstring 으로 변환 및 표시하기에 적합합니다. 이 포인터는 적어도 해당 예외 객체가 파괴되거나 예외 객체의 비상수 멤버 함수(예: 복사 할당 연산자)가 호출되기 전까지는 유효함이 보장됩니다.

상수 평가 중에는 반환된 문자열이 일반 리터럴 인코딩으로 인코딩됩니다.

(C++26 이후)

참고

구현체는 what() 을 재정의할 수 있지만 필수는 아닙니다.

std:: exception 로부터 상속됨

멤버 함수

[virtual]
예외 객체를 파괴함
( std::exception 의 virtual public member function)
[virtual]
설명 문자열을 반환함
( std::exception 의 virtual public member function)

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr 예외 타입

예제

#include <iostream>
#include <typeinfo>
struct S // The type has to be polymorphic
{
    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!