C ++ programming ideology-runtime type recognition

Source: Internet
Author: User

Two rtti usage methods:
1. The first type is typeid (), which is similar to sizeof and looks like a function, but is actually implemented by the compiler.
The typeid () parameter is an object reference or pointer that returns a reference to a constant object of the global typeinfo class. You can use = or! =.
The iso c ++ standard does not define typeinfo exactly. Its exact definition is related to the compiler, but the standard specifies that its implementation must provide the following four operations:

Typeinfo1 = typeinfo2 If the typeinfo1 and typeinfo2 objects are of the same type, true is returned; otherwise, false is returned.
Typeinfo1! = Typeinfo2 If the typeinfo1 and typeinfo2 types of the two objects are different, true is returned; otherwise, false is returned.
Typeinfo. Name () You can use name () to obtain the type name. Example: cout <typeid (* s). Name ()
Typeinfo1.before (T2) You can use before (typeinfo &) to query whether a typeinfo object is before another typeinfo object (in the inheritance order ).

When the operand of the typeid operator is a class type without a virtual function, the typeid operator specifies the type of the operand rather than the type of the underlying object.
If the operand of the typeid operator is a class type that contains at least one virtual function and the expression is a base class application, the typeid operator indicates the type of the derived class of the underlying object.

Typeid can be applied to non-polymorphism types, but the information obtained by this method is questionable.

2. The second type is "dynamic_cast security type downward ing"
You can use the static C ing static_cast of C ++ to force the execution, but this is dangerous because there is no way to clearly know what it actually is.
So use dynamic_cast
Shape * sp = new circle;
Circle * CP = dynamic_cast <circle *> (SP );
If (CP) cout <"CAs successful"; // If an address is returned successfully, null is returned, indicating that it is not a circle * object.

Dynamic ing can be used not only to determine accurate types, but also to intermediate types in multi-level inheritance relationships.
Typeid always generates a reference to a typeinfo object to describe the exact type of an object, so it does not provide intermediate level information.
The pointer type seen by typeid is a base class, and the reference type seen by typeid is a derived class.
The pointer that typeid sees points to a derived class, And the referenced address type that it sees is a base class.
During Multi-inheritance, if a base class pointer actually pointing to a subclass is forcibly converted to a subclass pointer, an error will also occur.

------------------------------ Two bad-cast -----------------------------------
1. dynamic_cast converts a completely unrelated class.
2. operate a null pointer using typeid

---------------------------- Other syntaxes -----------------------------------
Static_cast: ing used for "good behavior" and "good behavior", including some mappings that we may not need now (such as up ing and automatic type conversion ).
Const_cast: used to map constants and variables (const and volatile)
Reinterpret_cast: To map to a completely different meaning. This is the most dangerous of all mappings.

Related Article

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.