【发布时间】:2015-12-28 11:07:22
【问题描述】:
我正在尝试使用指针自己实现节点列表。 所以在我的课堂上将此作为私人:
class cj_enters {
private:
struct node{
node* next;
node* prev;
int info;
};
node* first;
node* last;
node* actual;
int size;
public:
.
.
};
我正在尝试实现这个方法:
bool cj_enters:: operator==(const cj_enters& B) const{
bool cerca = true;
node* aux = first;
while(aux != NULL and cerca){
if(!B.conte(aux->info)) // this method is working and it tells if object B has the info somewhere of the current node info(aux).
{
cerca = false;
}
else aux = aux -> next;
}
return cerca;
}
所以我不知道为什么,但是当我比较这些对象中的两个时,这个方法会返回奇怪的值,比如 244 左右,它们是否相同并不重要。
这是为什么呢?谢谢
【问题讨论】:
-
node* aux = first;和delete aux;似乎不会导致定义的行为...... -
你能指定这个 operator== 应该做什么吗?为什么在 operator== 中使用 delete 语句?此外,请避免将 else 语句放在同一行且不带括号。
-
如果 this 和 B 有相同的“第一”节点,那么它们的数据是相同的,但是它们也有一些遍历状态,所以你想知道它们是否都在迭代中的同一点或不?或者你可以直接返回 this.first == B.first;
-
operator == 应该在类的两个对象之间进行比较,如果它们在它们的节点中包含相同的信息,因此大小必须等于 ofc。
-
@KennyOstrom 他们不分享相同的节点,我们只是比较他们节点上的信息。