如何使用 IsKindOf() 函数检查两个对象?
我知道 ISKINDOF() 函数用于比较声明为 DECLARE_DYNAMIC 的两个类对象。
但问题是如何比较未声明DECLARE_DYNAMIC的对象指针。
CSample *sample1, *sample2;
sample1.ISKINDOF(sample2);
上面的示例肯定会显示错误。
有没有函数可以检查上述约束?
I know that ISKINDOF() function is used to compare two class objects that is declared DECLARE_DYNAMIC.
But the problem is how to compare the object pointers that is not declared DECLARE_DYNAMIC.
CSample *sample1, *sample2;
sample1.ISKINDOF(sample2);
definetely the above sample will show error .
Is there any function to check the above constraints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,此解决方案确实需要从 CObject 派生并使用
DECLARE_DYNAMIC
和IMPLMENT_DYNAMIC
。有什么原因你不能使用它们吗?获取应与其他对象相同或超类的对象的运行时类。假设您认为 Sample2 是 Sample1 的同一类或超类:
Note, this solution does require deriving from CObject and using
DECLARE_DYNAMIC
andIMPLEMENT_DYNAMIC
. Is there some reason you can't use them?Get the Runtime Class of the object that should be the same as or superclass of the other object. Assuming you think sample2 is the same class or a superclass of sample1:
如果你的 VC++ 代码是 /GR 编译的(这是最近编译器/IDE 中的默认设置),那么你可以使用dynamic_cast 来探索对象的运行时类型。
然而,正如 DWO 在他的评论中暗示的那样,使用 ISKINDOF 等人的设计通常应该替换为其他设计(例如虚拟函数)。
马丁
If your VC++ code is compiled /GR (which is the default in recent compilers/IDEs) then you can use dynamic_cast to explore the runtime type of objects.
However, as DWO hints in his comment, designs that use ISKINDOF et al should often be replaced with other designs (such as a virtual function).
Martyn