[英]Increment ++ Operator overloading in linked list
我想通过将current = current->next
更改为current++
来增加下一个节点,但它不起作用。 所以我尝试使用迭代器 class 来执行运算符重载。 但是有一个错误,我不确定这里会发生什么:
struct node{
int value;
node *next;
}
class Iterator{
public:
Iterator();
Iterator(node *);
Iterator operator++(int);
private:
node *point;
};
Iterator::Iterator(){
point = NULL;
}
Iterator::Iterator(node *current){
point = current;
}
Iterator Iterator::operator++(int u){
point = point->next;
return *this;
}
和打印是这样的:
class linkedList{
public:
void print() const;
protected:
node *first;
}
void linkedList::print()const{
node *current = first;
Iterator p = Iterator(current);
while(current != NULL){
cout << current->value << " ";
p++;
}
}
但事实证明有一个错误
你不能有一个一半使用指针一半使用迭代器的循环。 选择一个或另一个。 例如使用迭代器
Iterator p = Iterator(current);
while (current != Iterator()){
cout << p->value << " ";
p++;
}
现在这个循环意味着你还必须为你的迭代器重载operator!=
和operator->
。 重载operator==
和operator*
也是有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.