Understanding dynamic cast

Source: Internet
Author: User
Dynamic_cast is a run-time type conversion operator and it ensures only valid conversion can success. The expression dynamic_cast (SRC) converts expression SRC to dest_type if appropriate, fails otherwise. Unlike static_cast, it performs type checking at run-time, so it incurs some performance penalty.
Static_cast checks if the conversion is valid at compile time. Given this expression static_cast (SRC), static_cast knows Declaring type Of SRC, with this information static_cast checks if it can be converted to dest_type. if SRC is actually referring to another type other than SRC is declared to be, static_cast still considers src to be of its declaring type. so you might get unexpected behavior at run-time.
Dynamic_cast takes the actual type of SRC into consideration. This is achieved with the help of rtti (run-time type information) [http://en.wikipedia.org/wiki/ Run-time_type_information] Which is only available to polymorphic classes. In other words, classes with at least one virtual function defined or inherited. But why?
There are several reasons. first, it has to be compatible with C. it's clear that rtti must be saved somewhere for each object. if we store the information in every object itself, the layout of object won't be compatible C object model any more because C ++ internally add some new fields. second, not everyone want to use dynamic_cast in their code. is it fair for all of them to suffer the increased Object size and subsequent performance penalty? So rtti is designed to be enabled only when necessary.
Polymorphism isn't an available feature in C, and it's the source where programmer want to use dynamic_cast. so it's wisely chosen to hold rtti. the address of a class's rtti object is placed in virtual table of the class.

There are two requirements for a dynamic_cast to success.CompleteAndUnambiguous.

1. Complete
The SRC must refer to or contain a complete object of target type. for example, if SRC refers to an concrete object of base type and you try to cast it to derived type, it will fail.

2. unambiguous
The SRC must contain a unambiguous object of target type. Take multiple inheritance for instance, it's necessary to explicitly points out which base class to convert to with scope resolution operator.

Casting in C ++: bringing safety and smartness to your programs http://www.acm.org/crossroads/Xrds3-1/ovp3-1.html

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.