我编译了以下示例:
#include <iostream>
#include <iterator>
using namespace std;
class myiterator : public iterator<input_iterator_tag, int>
{
int* p;
public:
myiterator(int* x) :p(x) {}
myiterator(const myiterator& mit) : p(mit.p) {}
myiterator& operator++() {++p;return *this;}
myiterator& operator++(int) {myiterator tmp(*this); operator++(); return tmp;}
bool operator==(const myiterator& rhs) {return p==rhs.p;}
bool operator!=(const myiterator& rhs) {return p!=rhs.p;}
int& operator*() {return *p;}
};
int main () {
int numbers[]={10,20,30,40,50};
myiterator beginning(numbers);
myiterator end(numbers+5);
for (myiterator it=beginning; it!=end; it++)
cout << *it << " ";
cout << endl;
return 0;
}
从cplusplus.com/reference我得到编译器警告:
iterator.cpp: In member function 'myiterator& myiterator::operator++(int)':
iterator.cpp:13: warning: reference to local variable 'tmp' returned
这里有什么问题?后缀签名是否应该是myiterator operator++(int)
按值返回?
是否在某个地方定义了 STL 迭代器上的后缀签名应该是什么样的?