1

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;
    }    
};
8
  • 1
    can you perhaps use just a single value int myval and set it using myval = val ? val1 : val2; in the constructor? Commented Jan 27, 2016 at 19:55
  • "set once at start-up" Are they known at compile-time? Related: Can you use C++11? Commented Jan 27, 2016 at 19:56
  • 1
    Don't engage in premature optimizations. Branch prediction makes wonders. That being said, you can have a pointer to one or another, set it at construction time and than derefrence for return. Commented Jan 27, 2016 at 19:56
  • yes I can use c++11. the value is not known at compile time as it is based on userinput. both myval1 and myval2 need to be stored separately as they have other uses. Commented Jan 27, 2016 at 19:57
  • Ok, that info was originally missing. You can set a pointer to the int you are interested in depending on the bool param and have GetMyValue return *ptrToCorrectMyVal Commented Jan 27, 2016 at 20:00

2 Answers 2

1

Use a pointer:

class MyValue
{
   int* myVal;
   int myval1; //often updates
   int myval2; //often updates

  myValue(bool val, int val1, int val2)
  {
     if (val) { 
         myVal = &myval1; 
     } else { 
         myVal = &myval2 
     }
     myval1 = val1;
     myval2 = val2;
  }

  int GetMyValue() //often called
  {
     return *myval;
  }

};

(or even better a reference as in Rabbid76 answer)

Sign up to request clarification or add additional context in comments.

4 Comments

I will prefer not to introduce a new global variable. can function objects help in this situation?
I think you would still need a variable to store the function object instance
i am ok with storing function object instance.
@bsobaid: How is storing a function object instance any better than storing an int*?
1

Use a member which is either a reference to myval1 or to myval2, the referenc has to be initialized once in the constructor:

class MyValue
{
    bool myVal; //set only once in the constructor
    int myval1; //often updates
    int myval2; //often updates
    int &valref;

public:
    MyValue( bool val, int val1, int val2 )
        : myVal( val )
        , myval1( val1 )
        , myval2( val2 )
        , valref( val ? myval1 : myval2 )
    {}

    int GetMyVal() { return valref; }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.