functor - c++ code refactoring using function objects -


i have functionality returns value based on values set once @ start (in constructor). these conditional value set once, dont want checking them time. there way if-check once , dont again , again using function objects or other mechanism?

class myvalue {     bool myval; //set once in constructor     int myval1; //often updates     int myval2; //often updates       myvalue(bool val, int val1, int val2)     {         myval = val; // place myval set          // value changes in other functions not shown here         myval1 = val1;          // value changes in other functions not shown here         myval2 = val2;     }      int getmyvalue() //often called     {         if(myval)    /* there way dont have if check here?                          , write return statement? */             return myval1;          return myval2;     }     }; 

use 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 better reference in rabbid76 answer)


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -