std::bad_typeid - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::bad_typeid

From cppreference.com
< cpp‎ | types

Defined in header `<typeinfo>` |  |
class bad_typeid : public std::exception; |  |

An exception of this type is thrown when a `typeid` operator is applied to a dereferenced null pointer value of a polymorphic type.

Inheritance diagram

### [edit] Member functions

(constructor) | constructs a new `bad_typeid` object (public member function)

## Inherited from std::exception

### Member functions

(destructor) [virtual] | destructs the exception object (virtual public member function of `std::exception`) [edit]
what [virtual] | returns an explanatory string (virtual public member function of `std::exception`) [edit]

### [edit] Example

Run this code

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

Output:

    Attempted a typeid of NULL pointer!

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/types/bad_typeid&oldid=66458"