The best way to have public variable

Hi,

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// main.cpp
int main() {
   // ... code
   if(state_c = 0) { // check the value of the public variable
      //... code to be executed
      }
   else if(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
   }
   else if(state_c = 1) {
      //... code to be executed
   }

}


I cannot use macro here. I thought I could declare extern int 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!
I thought I could declare extern int state_c; in a header file.

You can.

But I have to initiate its value in each cpp file.

You have to define it in one cpp file.
if you really need this kind of global you could use a very thin singleton class:
http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C

OR, you could just declare 'state_c' in main() and pass this into your analyse() method, which would make more sense.
Why not just pass it as a parameter to analyze, or split it into two functions? This makes no sense.
and as an aside, your if statements should contain double ='s, not just one.
and your main should be returning an int.

tbh though i have no idea what you're trying to achieve with 2 identical if else code blocks.
mutexe wrote:
tbh though i have no idea what you're trying to achieve with 2 identical if else code blocks.
He's trying to demonstrate his misunderstanding of some concepts to us so we can explain how to properly understand them.
oh right. I misunderstood :)
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.)

Any thought, please?
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.

Maybe you need to reconsider your design?
What is configuration setting? It is the first time I heard this notion. I'll check online to read the explications...
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.
hen... I will try to understand that! Thanks!
if you really need this kind of global you could use a very thin singleton class:
http://www.codeproject.com/Articles/1921/Singleton-Pattern-its-implementation-with-C


have a look at that.
Public variable? Just say no.

You might consider having a general config class where objects can go to do determine run time parameters. This could be accessed via a singleton.

But public data? Or even protected data, just say no.
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.
Last edited on
Topic archived. No new replies allowed.