重载==不适用于二进制树

Overloaded == is not working for binary tree

本文关键字:二进制 不适用 重载 适用于      更新时间:2023-10-16
bool Tree::operator==(const Tree &that) const {
return Node::is_equal(this->getRoot(), that.getRoot());
}
bool Tree::operator!=(const Tree &that) const {
return !Node::is_equal(this->getRoot(), that.getRoot());
}

int main(){
Tree *t = new Tree();
const Tree *t2 = new Tree();
cout << (t == t2) << endl;
return 0;
}

重载的==函数没有被调用,我不确定为什么?相反,它只是比较内存中的地址。

在中

Tree *t = new Tree();
const Tree *t2 = new Tree();
cout << (t == t2) << endl;

您比较Tree的指针,要调用您的运算符,您需要比较Tree

Tree t, t2;
cout << (t == t2) << endl;

或者使用您的指针来取消引用:

Tree *t = new Tree();
const Tree *t2 = new Tree();
cout << (*t == *t2) << endl;