具有迭代器和const_iterator的取消引用运算符

Dereference operator with iterator and const_iterator

本文关键字:取消 引用 运算符 iterator 迭代器 const      更新时间:2023-10-16

我一直在努力解决与开发模板类迭代器有关的问题。更具体地说,以正确的方式实现去引用运算符(运算符*()),以便模板类涵盖迭代器和const_iterator的情况。我确信我遗漏了一些明显的东西,但我看不见。你能帮我吗?

假设我有以下模板类(Iterator.hpp),我想用它来迭代类的对象,这些类将STL容器(std::map,std::vector)封装为私有成员。

#include <iostream>
#include <iterator>
template<typename iterator_type>
class Iterator
{
  public:
    /** brief Type to be returned when de-referencing the iterator*/
    typedef typename std::iterator_traits<iterator_type>::value_type value_type;
    /** brief Constructor*/
    inline Iterator(iterator_type i) : iterator(i) {}
    /** brief Dereference operator */ 
    inline value_type& operator*() {return *iterator;}
    inline const value_type& operator*() const {return *iterator;}
    /** brief Increment operator */
    inline Iterator & operator++() {++iterator; return *this;}
    /** brief Inequality operator */
    inline bool operator!=(const Iterator & right) const
                                            {return iterator != right.iterator;}
    /** brief Inequality operator */
    inline bool operator!=(const iterator_type & right) const
                                                     {return iterator != right;}
    /** brief Distance between iterators */
    inline int operator-(const Iterator & right) const
                               {return std::distance(right.iterator, iterator);}
    /** brief Distance between iterators */
    inline int operator-(const iterator_type & right) const
                               {return std::distance(right, iterator);}
  private:
    /** brief Internal member, of iterator type*/
    iterator_type iterator;
};

现在假设我有一个main(example.cpp),它创建一个int向量,并尝试使用const_iterator打印它们,如下所示:

#include "Iterator.hpp"
#include <vector>
typedef std::vector<int> IntVector;
int main(int argc, char* argv[]) {
    unsigned int nElements(10);
    IntVector intVector(nElements);
    for (unsigned int i = 0; i < nElements; ++i) {
        intVector[i] = i;
    }
    std::cerr << "    Printout of the vector n";
    Iterator<IntVector::const_iterator> it(intVector.begin());
    for(; it != intVector.end(); ++it) {
        std::cerr << *it << "n";
    }

}

如果我尝试编译这个代码:g++ example.cpp -std=c++11 -stdlib=libc++。我将得到以下错误:

./Iterator.hpp:16:48: error: binding of reference to type 'value_type' (aka 'int') to a value of type 'const int' drops
      qualifiers
        inline value_type& operator*() {return *iterator;}
                                               ^~~~~~~~~
example.cpp:19:22: note: in instantiation of member function 'Iterator<std::__1::__wrap_iter<const int *> >::operator*'
      requested here
        std::cerr << *it << "n";
                     ^
1 error generated.

我认为,只需像Iterator.hpp模板中那样用常量和非常量版本重载operator*()就足够了,但事实显然并非如此。你知道我在这里缺了什么吗?如果能在这个问题上提供任何帮助,我们将不胜感激。

非常感谢!

const_iteratoriterator是不同的类型,应该作为单独的类型来实现。const_iterator应该在其解引用运算符中返回一个const引用,普通迭代器返回可修改的引用。

不应将const_iteratorconst iterator混淆。