预增量运算符过载

Overloading Pre-Increment Operator

本文关键字:运算符      更新时间:2024-05-10

以下是声明:

Iterator operator++();//pre-increment

以下是定义:

LinkedList::Iterator& LinkedList::Iterator::operator++(){
return Iterator(current->next);//this is giving me an error
}

以下是该类的外观

class LinkedList{
public:
Node struct{
/*contains pointer to next node and an integer value*/
int val;
Node* next;
};
class Iterator{
public:
Iterator& operator++();//pre-increment
protected:
Node* current;//points to current node
}
} 

创建一个新的迭代器对象,并(尝试(返回对该对象的引用。

前缀增量运算符修改this对象,并应返回对其自身的引用:

current = current->next;  // TODO: Add checking for not going out of bounds or dereferencing a null pointer
return *this;