c++ - 运算符 != 不匹配(操作数类型为指针和对象)
问题描述
我重载了==
and!=
运算符,并希望后者引用前者,以便根本不重复任何代码。这是我写的:
bool Date :: operator == (const Date & other) const {
bool are_equal = Year() == other.Year();
for (int i=0; i<other.NumEvents() && are_equal; i++)
are_equal = this[i] == other[i];
return are_equal;
}
bool Date :: operator != (const Date & other) const {
return !(this == other);
}
这里最大的问题this
是不是 aDate
而是 a Date*
。有没有办法在this Date
没有指针或使用this
with的情况下引用other Date
?
解决方案
取消引用指针:
return !(*this == other);
推荐阅读
- web - 如何改进浏览器缓存
- powershell - 停止远程使用文件的进程
- c# - 如何在从网络摄像头获取的 c# Emgu cv 帧中创建服务器 RTSP?
- jquery - 如何反转滚动运动?
- c# - 如何只用剃刀返回 html 正文的内容?
- java - 是否可以向 ClassPathXmlApplicationContext 添加相对路径?
- powershell - 带有 MSOnline 模块的任务计划程序脚本
- ios - Ionic 4 - 当我在 iOS 中运行应用程序时,Api 调用不起作用
- ruby-on-rails - Order multiple images by filename in Active storage Rails 5.2
- typescript - NestJS 5 GraphQL 错误查询在解析器中定义,但不在模式中