我在告诉运算符[]的get和set之间的区别时遇到了麻烦.我需要告诉这些函数调用之间的区别.
cout << data[5];
data[5] = 1;
Run Code Online (Sandbox Code Playgroud)
我用Google搜索了,我找到的答案仍然无济于事.人们建议通过添加const来使方法的签名不同.我做到了,他们仍然都使用相同的方法.
有我使用过的签名:
const T& operator[](unsigned int index) const;
T& operator[](unsigned int index);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试实现一次用于Set的运算符[]和一次用作Get,我需要区分这两种情况,就像get的情况一样,如果返回的值相等,我需要抛出一个异常到-1; 而在Set i的情况下,只需覆盖该值.appple [2] = X; Y =苹果[2];
我不知道如何区分这两种模式,我的功能签名是:
double& Security::operator[](QuarterType index){
if(index<0 || index>MAX_QUATERS){
throw ListExceptions::QuarterOutOfBound();
}
return evaluation[index].getValueAddress();
}
Run Code Online (Sandbox Code Playgroud)