c++ - 运算符 != 不匹配(操作数类型为指针和对象)

标签 c++ c++11 pointers operator-overloading this

我重载了 ==!= 运算符,并希望后者引用前者,以便完全不重复任何代码。这是我写的:

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 或将 thisother Date 一起使用?

最佳答案

取消引用指针:

return !(*this == other);

关于c++ - 运算符 != 不匹配(操作数类型为指针和对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678717/

相关文章:

c++ - C++ 函数参数中的指针与引用

c++ - 跨两个 DLL 的 `using` 语句

c++ - integral_constant 的编译时 bool 运算?

c++ vector 的最小值大于另一个值

c++ - 避免循环引用

c++ - 在 C++ 中返回一个 std::vector

C++ 我的字符串类 : Pointer doesn't work

c++ - 重新排序和 memory_order_relaxed

c++ - 在方法 c 中传递双指针

c++ - 传递指针以更改其值但保持不动