Dynamic_cast, RTTI, sorting

Source: Internet
Author: User

The main reference is to understand the memory layout, and then write an instance program to understand it, but you need to be familiar with pointer conversion.

1) only polymorphism classes have RTTI information. dynamic_cast uses RTTI for conversion and is a runtime type check.

2) When determining whether two pointers can be converted using dynamic_cast, you can use RTTI to know the current actual object and traverse all of its parent classes to see if there is any type consistent with the target, if yes, it can be converted.

3) dynamic_cast is safe. You can check the return value or exception capture to determine whether the conversion is successful. The return value is used for pointer conversion, and the exception capture is used for reference conversion.

4) In addition, unlike static_cast, even if the two classes do not have a direct inheritance relationship, as long as they are in a class hierarchy, they may point to the same object, and dynamic_cast can be performed. For example, C inherits from A and B. A and B pointers can be used for dynamic_cast and may be successful.

C * pc = new C;
A * pa = pc;
B * pb = pc;

Pb = dynamic_cast <B *> (pa); // The conversion is successful because both point to the C object.

 

 

 

The following example is mainly about RTTI, which prints the runtime information of an object and all its parent classes. The runtime information here is mainly the class name.

#include <typeinfo>   Base :   Deri1234567890ve :  mdisp;       pdisp;       vdisp;   TypeDescriptor* pTypeDescriptor;     DWORD numContainedBases;      PMD ;            DWORD attributes;            DWORD attributes;         DWORD numBaseClasses;      RTTIBaseClassArray*     TypeDescriptor* pTypeDescriptor;      RTTIClassHierarchyDescriptor* pClassDescriptor; *pderive =  *ptable = (*)*(* * rtti = ptable -    RTTICompleteObjectLocator * RIIT_locator =   (RTTICompleteObjectLocator *)( *(*<<RIIT_locator->pTypeDescriptor->name<<     * p1 = (*)(RIIT_locator->pClassDescriptor-> * p2 = (*)*(p1+* pDesc = (TypeDescriptor*)(*<<<<pDesc->name<<= (*)(RIIT_locator->pClassDescriptor->= (*)*(p1 + = (TypeDescriptor*)(*<<<<pDesc->name<<= (*)(RIIT_locator->pClassDescriptor->= (*)*= (TypeDescriptor*)(*<<<<pDesc->name<<

 

Refer:

Discussion on Dynamic_cast and RTTI http://www.cnblogs.com/zhyg6516/archive/2011/03/07/1971898.html

Http://www.openrce.org/articles/full_view/23

 

 

Asdfasdf

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.