索引运算符过载
Overloading indexing operator
我正在尝试重载索引运算符[]
。我有两个版本我需要同时使用它们,但当我构建项目时,会调用其中一个,并且无论在哪里使用第二个:Invalid operands to binary expression double and Quarters
,都会出现错误。
这是第一个:
Quarters& Security :: operator [] (QuarterType quarter){
return quartersData[static_cast<int>(quarter)];
}
第二个:
const double& Security :: operator [] (QuarterType quarter) const{
return (quartersData[static_cast<int>(quarter)].getPrediction());
}
问题出在哪里?我能做些什么来解决它?
重写不考虑返回类型。由于在这两种情况下,参数类型都是相同的,因此重写只考虑对象的const
性。如果您的对象不是const
,那么无论您下一步要对它做什么,都将调用operator[]
的非常量版本。
相关文章:
- 重载元组索引运算符-C++
- 重载运算符 [] 用于从对象数组中给出特定索引
- 为什么函数的任何索引处的下标运算符在C++中总是返回1
- std::map 索引运算符与插入方法的性能
- 为什么我不能使用在 Visual C++ 32 位中实现运算符无符号 int() 作为数组索引的类?
- 在对象上运行,运算符重载 () 与索引
- 为什么在C 中使用索引运算符被认为是不好的样式
- <T> 在使用运算符+ 连接两个向量之前从类型向量中删除索引
- 当c 中的矢量容器过载[]运算符时,我对未定义的索引返回什么
- String_view的索引运算符([])之间的差异
- 为什么在 std::vector 中使用索引超出范围的运算符 [] 时没有出现异常?
- C++将索引器运算符添加到现有类而不进行修改
- 通过赋值运算符在索引处插入std::向量
- C++数组运算符和 *(数组 + 索引)之间有什么区别(如果有)
- c++中线程安全索引运算符的正确方法
- 堆中的对象和重写索引运算符
- 索引运算符的 deques 和超出 deque 大小的问题
- 数组索引运算符重载.现在无法使用比较运算符
- 多次调用索引运算符的性能
- 是否可以使用运算符重载按索引分配用户定义的数组?-C++