MSDN中DYNAMIC_DOWNCAST: The macro will cast the pointer parameter to a pointer to an object of the class parameter's type.If the object referenced by the pointer is a "kind of" the identified class, the macro returns the appropriate pointer. If it is not a legal cast, the macro returns NULL.源码中#define DYNAMIC_DOWNCAST(class_name, object) \ (class_name*)AfxDynamicDownCast(RUNTIME_CLASS(class_name), object)CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject) { if (pObject != NULL && pObject->IsKindOf(pClass)) return pObject; else return NULL; }
功能一样,但本质存在区别:dynamic_cast是C++关键字,标准支持,用在类体系中 MSDN中dynamic_cast:Converts the operand expression to an object of type type-id: dynamic_cast < type-id > ( expression ) In Visual C++ 2005, there are two breaking changes in the behavior of dynamic_cast in managed code: dynamic_cast to a pointer to the underlying type of a boxed enum will fail at runtime, returning 0 instead of the converted pointer.dynamic_cast will no longer throw an exception when type-id is an interior pointer to a value type, with the cast failing at runtime. The cast will now return the 0 pointer value instead of throwing. 从上面源码看出DYNAMIC_DOWNCAST是在MFC中,用在MFC类体系中,是函数调用
(class_name*)AfxDynamicDownCast(RUNTIME_CLASS(class_name), object)CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject)
{
if (pObject != NULL && pObject->IsKindOf(pClass))
return pObject;
else
return NULL;
}
但功能远远没有dynamic_cast好.侯捷的<<深入浅出MFC>>上有很详细的解释.
MSDN中dynamic_cast:Converts the operand expression to an object of type type-id:
dynamic_cast < type-id > ( expression )
In Visual C++ 2005, there are two breaking changes in the behavior of dynamic_cast in managed code: dynamic_cast to a pointer to the underlying type of a boxed enum will fail at runtime, returning 0 instead of the converted pointer.dynamic_cast will no longer throw an exception when type-id is an interior pointer to a value type, with the cast failing at runtime. The cast will now return the 0 pointer value instead of throwing.
从上面源码看出DYNAMIC_DOWNCAST是在MFC中,用在MFC类体系中,是函数调用