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.