I got a Security Class that has an array of Predictions - Prediction is a Class, which holds only a double.
I want to allow changing the value of the double, but allow only positive values,
and when trying to read the double, if the value is uninitialized (equals -1 in my code) throw exception.
I also have double operator in
Something like that:
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;
where do I define between reading and writing, because I'm doing different things when reading and writing.
THANKS
Using a combination of a conversion operator and a conversion constructor you can get this behavior. This sample code should give you an idea on how you need to implement you classes:
Output: