DYNAMIC_DOWNCAST and dynamic_cast

Source: Internet
Author: User

Dynamic_cast <type-id> (expression) This operator converts expression to type-id objects. Type-id must be a class pointer, class reference, or void *. If type-id is a class pointer Type, expression must also be a pointer, if type-id is a reference, expression must also be a reference. The dynamic_cast operator can determine the real type during the execution period. If downcast is secure, if the base class pointer or reference actually points to a derived class object, the operator returns a pointer that has been transformed as appropriate. If downcast is not secure, this operator returns a null pointer, that is, a base class pointer or a reference that does not point to a derived class object ). Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes. When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect. During downstream conversion, dynamic_cast has the type check function, which is safer than static_cast. Class B {public: int m_iNum; virtual void foo () ;}; class D: public B {public: char * m_szName [100] ;}; void func (B * pb) {D * pd1 = static_cast <D *> (pb); D * pd2 = dynamic_cast <D *> (pb);} in the code snippet above, if pb points to a D-type object, pd1 and pd2 are the same, and it is safe to execute any operations of the D-type on these two pointers; however, if pb points to a B-type object, pd1 will be a pointer to the object, and the D-type operation on it will be insecure, such as accessing m_szName ), pd2 is a null pointer.

DYNAMIC_DOWNCAST (class, pointer) parameter: class name. Pointer will be forcibly converted to the pointer of the class Object pointer. Note: The DYNAMIC_DOWNCAST macro provides a simple method to forcibly convert a pointer to a Class Object and check whether this forced conversion is legal. This macro will forcibly convert the pointer parameter to the object pointer of the class parameter type. If the pointer parameter points to an object of the class, this macro returns an appropriate pointer. If the conversion is invalid, the macro returns NULL.

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.