Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Okay to compare pointers returned by RUNTIME_CLASS() macro?

Tags:

c++

rtti

mfc

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() macro 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?

like image 924
Kurt Hutchinson Avatar asked Jun 25 '26 16:06

Kurt Hutchinson


2 Answers

No such guarantee is documented, albeit that it is likely. You are supposed to use CObject::IsKindOf().

like image 54
Hans Passant Avatar answered Jun 29 '26 19:06

Hans Passant


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.

like image 36
Aoi Karasu Avatar answered Jun 29 '26 19:06

Aoi Karasu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!