【发布时间】:2013-11-17 14:20:04
【问题描述】:
请帮助我,在双向链表中实现重载运算符++。 我有 A 和 B 两个班级。
class A {
private:
int h;
int a;
public:
A *next, *prev;
friend A operator ++(A &, int);
};
A operator ++(A &t, int) {
A temp = t;
temp.h++;
temp.a++;
return temp;
}
class B {
private:
A *head, *tail;
public:
void incValue();
};
void B::incValue() {
while(head != nullptr) {
head++;
head = head -> next;
}
}
执行后方法incValue() head = NULL 我不明白为什么这不起作用。
附:此代码必须是 eq。头++
head -> setH(head -> getH() + 1);
head -> setA(head -> getA() + 1);
【问题讨论】:
-
while(head != nullptr)这会导致它循环直到你有head== 到nullptr(猜测)是NULL。当你把它当真时,你就打破了循环。因此,当你出来的时候 head 是 NULL -
head是一个数组吗?如果没有,你觉得head++在做什么? -
究竟是什么不起作用?
-
head 是列表中的第一个节点。我想让 head++ 做 head -> h = head -> h + 1
-
从设计的角度来看,有一个类似增量的运算符或成员函数来改变链表的状态并不是很好。最好为此实现迭代器。
标签: c++ linked-list operator-overloading doubly-linked-list