I have some functionality that returns a value based on values that are set once at start up (in constructor). As these conditional value are only set once, I dont want to be checking them all the time. Is there a way to do the if-check only once and then dont do it again and again using function objects or some other mechanism?
class MyValue {
bool myVal; //set only once in the constructor
int myval1; //often updates
int myval2; //often updates
myValue(bool val, int val1, int val2)
{
myVal = val; // only place where myVal is set
// this value changes often in other functions not shown here
myval1 = val1;
// this value changes often in other functions not shown here
myval2 = val2;
}
int GetMyValue() //often called
{
if(myval) /* Is there a way I dont have to do an if check here?
and simply write a return statement? */
return myval1;
return myval2;
}
};
int myvaland set it usingmyval = val ? val1 : val2;in the constructor?boolparam and haveGetMyValuereturn*ptrToCorrectMyVal