What is the best strategy for exchanging variables between source files in c / C ++?

I often have to write c / C ++ programs with 10+ source files in which it is necessary to distribute several variables between functions in all files. I read earlier that it is generally recommended to avoid using global variables with extern . However, if you need to use global variables completely, this link provides a good strategy. Recently, I have been playing with the strategy of wrapping all my variables in a structure or class and passing this structure to various functions. I was wondering how people consider themselves cleaner, and if there are any better alternatives.

EDIT: I understand that strategies can be different in two languages. I'm interested in strategies that apply only to one language or to both.

+8
c ++ c global-variables extern
source share
5 answers

Skip context class / data structure instead of global variables. You will be surprised how often a global variable becomes not global, and different modules want to use different values ​​for it at the same time.

+4
source share

The best alternative to globals is not to use global variables.

Do not try to sweep them under the rug using a structure or namespace or singleton or some other stupid thing whose sole purpose is to hide the fact that you are using global variables.

Just don't create it. Someday.

This will make you think about property and life, as well as addiction and responsibility. You know adult stuff.

And then, when it’s convenient for you to write global code, you can begin to break all these rules.
Because this is what the rules are for: follow and break.

+3
source share

My trick in C ++

Anyway, I found good practice in C++ to limit the scope of my global variables with namespace . This way you can eliminate any ambiguity between your 10+ source files.

For example:

 namespace ObjectGlobalVars { //Put all of your global variables here int myvariable = 0; } //And then later on you can reference them lik ObjectGlobalVars::myvariable++; 
+2
source share

DO I NEED TO BE GLOBAL?

This is the first question you should always ask if this GLOBALY variable is used, for example. in all contexts. The answer is almost certain ... no, it is not .

Consider the context

Is the variable a global state, or is it a context? The global state is usually rare, context, on the other hand, is quite common. If this global state considers wrapping in singleton mode, you can control how to interact with your global ones. Using Atomic<> is probably not a bad idea, you should at least consider synchronization.

If this is a context, then it must be explicitly passed in the structure or class, since the data is clearly not related to this context. A missing context may seem like a burden, but it shows very clearly where the context comes from, and not just refers to random variables from the air.

What is scope?

It may seem strange to say that global areas are covered by scope, but any global declaration declared in a single file can be declared static and thus cannot be deleted from any other file. This means that you can restrict access to global status in this area. This prevents accidental changes to variables.

+2
source share

In c ++
The presence of global variables lying here and there is an example of bad code.
If you want to share things globally, group them and follow the singleton pattern.

Example:

 class Singleton { private: int mData; public: static Singleton& getInstance() { static Singleton instance; return instance; } int GetData() { return mData; } private: Singleton() {}; Singleton(Singleton const&); void operator=(Singleton const&); }; 

<strong> Benefits:

  • Only one global variable. A copy of our singleton.

  • You can enable mutex / semaphore mechanisms inside a singleton, for thread-safe access to them.

  • Limits member access, helping you avoid logic and synchronization errors.

Disadvantages:

  • More difficult to implement. - If this is your first time -



In c
You should avoid declaring global variables; pass them in structures instead.

For example:

 struct MyData { int a; int b; }; void bar(struct MyData* data) { data->b = 2; } void foo() { struct MyData mdata; mdata.a = 1; bar( &mdata ); } 



To summarize
The presence of global variables lying around should be avoided as much as possible in both languages .

+1
source share

All Articles