Rtti (dynamic_cast conversion operator and typeid operator)

Source: Internet
Author: User

To use the rtti feature, you must include the <typeinfo> header file and use/GR to enable runtime information,

In Visual Studio

Open the "property page" dialog box for this project. For more information, see How to: Open the project properties page.

Click the "C/C ++" folder.

Click the language attribute page.

Modify the "enable runtime type information" attribute.

In G ++, the command/GR should be used during compilation. (I am not sure about this. I have never tried it)

First, let's take a short section.Code:

Class
{
Public:
Virtual ~ A ()
{}
};
Class B: public
{
};

1. The force conversion operator of dynamic_cast converts a pointer or reference of a base class to a pointer or reference of its subclass. The usage is dynamic <type> (object ). note: The base class must contain virtual functions (if there is no virtual function, you can declare the Destructor as virtual, which will be explained later ), the following situations may occur after the pointer or reference of the base class is used:

1. when a base class pointer is converted to a subclass pointer, if the base class Pointer Points to an object of its subclass, the conversion is successful and a pointer pointing to the subclass is returned; otherwise, if the conversion fails, a null pointer is returned. For example:

A * pA = new B ();

B * pb = dynamic_cast <B *> (PA );

Delete Pa;

Because PA points to a Class B object, the conversion is successful. In the following example, if the conversion fails, the NULL pointer is returned:

A p;
B * pb = dynamic_cast <B *> (& P );
If (PB = NULL)
{
Cout <"P is not a B object" <Endl;
}
Else
{
Cout <"Pa is a B object" <Endl;
}

Output: P is not a B object

2. when a base class is converted to a subclass reference, if the base class references a subclass object, the conversion succeeds and a subclass reference is returned. Otherwise, the conversion fails, dynamic_cast will throw a bad_cast exception. for example:

A p;

A & A = P;
Try
{
B & B = dynamic_cast <B &> ();
Cout <"A is a reference of type B" <Endl;
}
Catch (bad_cast)
{
Cout <"A is not a reference of type B" <Endl;
}

Because a references an object of type A, the conversion fails and a bad_cast exception is thrown. Therefore, the output of this Code section is: A is not a reference of type B.

Now I want to explain why the dynamic_cast operator must require virtual functions in the base class. The following analysis is my opinion:

The type information of an object is directed by the first slot pointer in the first slot of the virtual table, the virtual table is pointed by the vptr (virtual table pointer) in the object model. To obtain the object type information, the base class must have a vptr, that is to say, there is at least one virtual function, so that you can obtain the type information pointed to in the virtual table at runtime. Determines whether the base class pointer or reference points to a subclass object. For more information about the distribution of virtual tables and virtual table pointers in the object model, see inside the C ++ object model.

I don't know whether the above statements are clear.

II. The typeid operator is used to obtain the runtime information of a type or object as follows:

Typeid (Type-ID), typeid (expression), returns an object of the type const type_info &. The type_info class has several member functions:

Bool operator = (const type_info & RHs) const;
Bool Operator! = (Const type_info & RHs) const;
Int before (const type_info & RHs) const;
Const char * Name () const;

For the role of each member function, see msdn. This class has a member function const char * type_info: Name (),
You can use it to obtain the name of this type, for example:

A * A = new B ();
Cout <typeid (* A). Name () <Endl;

Output on vc2005: Class B, not class A, because a points to a B-type object.

In G ++ or other compilers, it is possible to output B, 1b, and other type names after name-mangling. However, it tends to be the original type name.

It is a null pointer or a wild pointer. When we want to view the type it points to, a bad_typeid exception will be thrown, for example:
A * pA = NULL;
Try
{
Cout <typeid (& pa). Name () <Endl;
Cout <typeid (* pA). Name () <Endl;
}
Catch (bad_typeid)
{
Cout <"* pa is a bad typeid" <Endl;
}

Output: Class A ** returns * pa is a bad typeid after wrapping.

The dynamic_cast operator only processes objects, that is, dynamic_cast <type> (object), while typeid can directly use types and objects as operands. objects can be basic types, including integer and floating point types, char type and so on, namely: typeid (type) or typeid (expression), most of which can be obtained at runtime, rather than at static compilation. As mentioned above, when can we use the typeid operator for the usage of typeid?

The following code is used to display information of each type:

void displayinfo (const string & RHs)
{< br> If (RHS = typeid (employee ). name ()
{< br> employee EMPL;
EMPL. display ();
}< br> else if (RHS = typeid (department ). name ()
{< br> Department dept;
Dept. display ();
}< br> else if (RHS = typeid (project ). name ()
{< br> Project proj;
proj. display ();
}< BR >}this will probably show its role, right?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.