简体   繁体   English

重载预增量运算符

[英]Overloading Pre-Increment Operator

Here is the declaration:这是声明:

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

Here is the definition:这是定义:

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

Here is how the class looks这是 class 的外观

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
    }
} 

You create a new iterator object, and (attempt to) return a reference to that.您创建一个的迭代器 object,并(尝试)返回对它的引用。

The prefix-increment operator modifies this object, and should return a reference to itself:前缀增量运算符修改this object,并应返回对自身的引用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM