C++ Typecasting (794255), страница 2
Текст из файла (страница 2)
Then the programmer can make use of dynamic_cast.(If the dynamic cast is successful, then the pointer will point to an object of a derived class or to a class thatis derived from that derived class.) But there are circumstances that the programmer (not often) wants toknow the prizes data-type. Then the programmer can use the typeid operator.The typeid operator can be used with:VariablesExpressionsData-typesTake a look at the typeid example:#include <iostream>#include <typeinfo>using namespace std;int main(){int *a, b;a = 0; b = 0;if (typeid(a) != typeid(b)) {cout << "a and b are of different types:\n";cout << "a is: " << typeid(a).name() << '\n';cout << "b is: " << typeid(b).name() << '\n';}return 0;}Note: the extra header file typeinfo.The result of a typeid is a const type_info&.
The class type_info is part of the standard C++ library andcontains information about data-types. (This information can be different. It all depends on how it isimplemented.)A bad_typeid exception is thrown by typeid, if the type that is evaluated by typeid is a pointer that ispreceded by a dereference operator and that pointer has a null value.That is all for this tutorial. In the next C++ programming tutorial we will take a look at pre-processors.Динамическая информация о типе.type_info:Stores information about a type. An object of this class is returned by the typeid operator (as a constqualified lvalue). Although its actual dynamic type may be of a derived class. It can be used tocompare two types or to retrieve information identifying a type.typeid can be applied to any type or any expression that has a type.If applied to a reference type (lvalue), the type_info returned identifies the referenced type.Any const or volatile qualified type is identified as its unqualified equivalent.A typedef type is considered the same as its aliased type.When typeid is applied to a reference or dereferenced pointer to an object of a polymorphic class type(a class declaring or inheriting a virtual function), it considers its dynamic type (i.e., the type of themost derived object).
This requires the RTTI (Run-time type information) to be available.When typeid is applied to a dereferenced null pointer, a bad_typeid exception is thrown.The lifetime of the object returned by typeid extends to the end of the program.bad_typeid:Type of the exceptions thrown by typeid when applied on a pointer to a polymorphic type which hasa null pointer value.Its member what returns a null-terminated character sequence identifying the exception.bad_cast:Type of the exceptions thrown by dynamic_cast when it fails the run-time check performed onreferences to polymorphic class types.The run-time check fails if the object would be an incomplete object of the destination type.Its member what returns a null-terminated character sequence identifying the exception.Some functions in the standard library may also throw this exception to signal a type-casting error.1. http://www.codingunit.com/c-tutorial-typecasting-part-12.
http://www.codingunit.com/cplusplus-tutorial-typecasting-part-2-rtti-dynamic_cast-typeidand-type_info3. http://www.cplusplus.com/reference/typeinfo/.