I have a class which has a number of static function to perform some calculation. However, before the calculation, I need to pass in a data to initialize some of the static data members. Currently I have an init(data) function and a clearResource() function which should be called before and after the use of the class. Is there a better way of doing that?
For example:
classA(){
static int a;
static init(int b) {
a = b;
}
static functionA(){
//perform something based on value of a;
switch(a){
}
}
}
int main(){
classA::init(5);
classA::functionA();
}
Thanks
initandclearResourcefor each object of your class or only once for all objects.wrap the state and the functioninto its own class that encapsulates all this information. Then the constructor/destructor of this new class will handle all the above automatically.functionA, both before entering and after completion. If a second thread tries to call the function while the first thread is inside, it will change the global while in the middle of the operation.