可以比较 RUNTIME_CLASS() 宏返回的指针吗?
我有一个函数,它需要 CRuntimeClass 指针列表来设置视图。如果使用已设置的相同类的列表调用该函数,我想返回而不执行任何操作。保存指针值并在下一次调用时比较它们当前正在工作,但我想验证这是合法的事情,而不是偶然发生的事情。也许我的 doc-search-fu 缺少,但我找不到任何地方可以保证从给定类的 RUNTIME_CLASS() 宏返回的指针值在程序的生命周期中是相同的。我能找到的最接近的是 CObject::GetRuntimeClass( )
:
每个 CObject 派生类都有一个 CRuntimeClass 结构。
这意味着指针值不应更改,但并未明确说明。有人对此有更具体的了解吗?或者有更好的方法来比较 CRuntimeClasses 吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管有可能,但没有记录这样的保证。您应该使用 CObject::IsKindOf()。
No such guarantee is documented, albeit that it is likely. You are supposed to use CObject::IsKindOf().
查看
afx.h
并进行一些调试表明RUNTIME_CLASS()
返回一个指向静态成员的指针:static CRuntimeClass class##class_name (可以在
DECLARE_DYNAMIC(class_name)
宏的定义中看到)。由于该成员是静态的,因此指向它的指针在运行时不会改变。换句话说,
static
是您的保证。Taking a peek at
afx.h
plus a little of debugging shows thatRUNTIME_CLASS()
returns a pointer to a static member:static CRuntimeClass class##class_name
(as it can be seen in the definition ofDECLARE_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.