I have a project, where I need to create a public variable (instead of macro) to decide which part of code to be execute. The value of the public variable (0 or 1) will be input by console. Its structure is as follows:
// main.cpp
int main() {
// ... code
if(state_c = 0) { // check the value of the public variable
//... code to be executed
}
elseif(state_c = 1) {
//... code to be executed
}
// ... code
analyze(); // call a function
}
// analyze.cpp
void analyze() {
// ... code
if(state_c = 0) {
//... code to be executed
}
elseif(state_c = 1) {
//... code to be executed
}
}
I cannot use macro here. I thought I could declare externint state_c; in a header file. But I have to initiate its value in each cpp file. In case I have several cpp that check the value of this public variable, it's probably not a good idea.
Could anyone please help me with this? Thanks a lot!
Thanks guys for your rapid replies. Actually it's more complicated than what I have demonstrated. (I thought with a simplified structure I can find a solution but finally I have to make it clearer.)
I need a global variable, with value 0 or 1, depending on two radio buttons on the GUI: if I click on button A, the value will be defined as 0; if button B, value = 1. I have a function to define the value according to choice on GUI.
Once it's defined, a lot of functions (around 20 here) will check its value to decide which part of code to execute. I know I can pass the value as an argument to each cpp, but I am trying to find a beautiful way to do this without modify everything. (BTW, as I use multi-thread for those functions, there is a limitation of number of arguments, sometimes I cannot add more argument.)
When you have one or more parameters that are common to a group of functions, that's a sign of a hidden class that wants to pop out and become a real class.
Make sure your functions are in a class and that the variable you need is a private data member.
Yes, the point is among all those functions who need the value of this variable, some of them belong to a class, some not. It is not easily modifiable considering the complexity of code.
I think it is not good to make a function as member of two class, right?
Is the variable a configuration setting? It should be in a configuration class. Other functions should not change their behavior based on the configuration setting - you should tell the functions to behave differently based on the configuration settings (or call different functions). Otherwise you have tight coupling, which is bad.
It has nothing to do with programming - a configuration setting is something the user changes to make the application work the way they want it to. The language choice they choose is a configuration setting, so is the volume.
But public data? Or even protected data, just say no.
And, frankly, the same goes for global variables. Using them is the very opposite of "beautiful" - it introduces some very ugly tight coupling between your code units that can cause problems with maintenance and debugging.
To the OP: you've already mentioned that your code is complex, so adding to that complexity with the kind of tight coupling introduced by the use of global variables is not a good idea.
Additionally, you've said that your application is multi-threaded. Adding a global variable that could, potentially,have more than one thread attempt to access it simultaneously, which will cause some nasty problems.
As L B says, if you're finding you need to pass a value around such a large number of functions/methods, it probably indicates that you haven't yet found the right design.