使用常量迭代器的运算符重载
Operator Overloading with Constant Iterators
我有一个常量迭代器类,其中包含以下用于重载多个运算符函数的方法
self_reference operator=( const SDAL_Const_Iter& src ) {
index = src.index;
return *this;
}
self_reference operator++() {
index = index + 1;
return *this;
}
self_type operator++(int) {
SDAL_Const_Iter results = *this;
++index;
return results;
}
index
变量的类型为 const int
。
我的编译器抱怨我试图修改一个常量对象(更具体地说,"错误 C2166:l 值指定常量对象"),我知道这一点;但是,我没有看到其他方法来重载这些函数。有人可以详细说明如何在不引起编译器问题的情况下编写这些重载吗?
我相信
问题出在const int
作为变量index
。
迭代器不应允许对容器数据进行非常量访问。但是,迭代器本身是可变的(它必须能够迭代)。将index
更改为int
应该可以解决问题。
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板