Okay to compare pointers returned by RUNTIME_CLASS() macro?
I've got a function that takes a list of CRuntimeClass
pointers in order to setup a view. I'd like to return without doing anything if the function is called with a list of the same classes that are already setup. Saving the pointer values and comparing them on the next call is currently working, but I want to verify that that's a legal thing to do, and not something that just happens to work. Maybe my doc-search-fu is lacking, but I can't find anywhere that guarantees the pointer value returned from the RUNTIME_CLASS() ma开发者_如何学Ccro for a given class will be the same for the life of the program. The closest I could find is in the docs for CObject::GetRuntimeClass()
:
There is one CRuntimeClass structure for each CObject-derived class.
That implies that the pointer value shouldn't change, but doesn't exactly state it. Does anyone have something a bit more concrete on that? Or is there a better way to compare the CRuntimeClasses?
No such guarantee is documented, albeit that it is likely. You are supposed to use CObject::IsKindOf().
Taking a peek at afx.h
plus a little of debugging shows that RUNTIME_CLASS()
returns a pointer to a static member: static CRuntimeClass class##class_name
(as it can be seen in the definition of DECLARE_DYNAMIC(class_name)
macro).
As the member is static, the pointer to it does not change during runtime. In other words static
is your guarantee.
精彩评论