具有迭代器和const_iterator的取消引用运算符
Dereference operator with iterator and const_iterator
我一直在努力解决与开发模板类迭代器有关的问题。更具体地说,以正确的方式实现去引用运算符(运算符*()),以便模板类涵盖迭代器和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_iterator
和iterator
是不同的类型,应该作为单独的类型来实现。const_iterator应该在其解引用运算符中返回一个const引用,普通迭代器返回可修改的引用。
不应将const_iterator
与const iterator
混淆。
相关文章:
- C++取消引用指针.为什么会发生变化
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 取消引用运算符不能重载
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++ 关于指针取消引用的技术问题
- 没有取消引用/解包对象的标准方法?
- 列表 iter 不取消引用 使用列表进行插入排序
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 取消引用结束指针到数组类型的一个
- 取消引用向量时出现问题
- 取消引用指向整数的指针时获得不同的结果
- C++:取消引用十六进制值,有点语法问题
- C/C++ 取消引用错误:在空检查之前取消引用
- 取消引用后C++空测试
- 立即取消引用unique_ptr
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 取消引用 void 指针以将值分配给结构
- boost_multi_index迭代器取消引用给出常量
- 矢量迭代器不能与 std::shared_ptr<> 取消引用
- C++我们可以取消引用此指针吗?如果是这样,那么如何,如果不是,那为什么?