【发布时间】:2015-06-12 15:26:29
【问题描述】:
我有一个具有预测数组的安全类 - 预测是一个类,它只包含一个双精度数。
我想允许更改双精度值,但只允许正值,
并且在尝试读取双精度时,如果该值未初始化(在我的代码中等于-1),则抛出异常。
我在
中也有双重运算符
类似的东西:
class Prediction{
double value;
public:
.....
Prediction::operator double() const {
return this->prediction;
}
Prediction::operator=(const double value){
...
//check value
}
}
class Security{
...
Prediction& Security::operator[](int index){
return predArray[index];
}
}
Prediction *predArray = new Prediction[4];
//default constructor set the value -1;
double a = predArray[0] //should throw an exception, because predArray[0] = -1
predArray[0] = 4; //should be O.K. because I want to change the value
predArray[1] = -4; //should throw exception, because trying to put negative value;
我在哪里定义阅读和写作,因为我在阅读和写作时做不同的事情。
谢谢
【问题讨论】:
标签: c++ overloading operator-keyword