我重载了 ==
和 !=
运算符,并希望后者引用前者,以便完全不重复任何代码。这是我写的:
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
不是Date
而是Date*
。有没有一种方法可以在没有指针的情况下引用 this Date
或将 this
与 other Date
一起使用?
最佳答案
取消引用指针:
return !(*this == other);
关于c++ - 运算符 != 不匹配(操作数类型为指针和对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678717/