C++ 中具有双重调度的运算符 ==
一个应该如何实现
operator==(const Base& base)
来比较子类,当调用时,调用将被正确分派
Base* base1 = new Derived1();
Base* base2 = new Derived2();
base1->operator==(*base2)?
How should one implement
operator==(const Base& base)
to compare subclasses s.t. the calls would be properly dispatched when called as
Base* base1 = new Derived1();
Base* base2 = new Derived2();
base1->operator==(*base2)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会让您到达调用的位置
。从这里您可以选择
如果导出的数量很小且有限,则可以实现
为虚方法。在 Derived1 中,您可以覆盖并比较真实值。
That gets you to the point where you have
Called. From here you have some options
If the number of derived is small and finite, you can implement
as virtual methods. In Derived1, you override and compare for real.
这看起来像是关于类和类型的通用 C++ 问题,而不是关于操作符 == 的特定问题。据我所知,在您给出的特定示例中,除了使用 dynamic_cast
This seems like a generic C++ question on classes and type rather than a specific question on the operator== . Up to my knowledge, in the particular example you are giving there is no other way but to use dynamic_cast