简体   繁体   中英

Is it OK to use global-scope objects (structs) in C++ (C)?

As we all know, it is a bad habit to use global variables unnecessarily and there is a tendency to keep the scope as small as possible.

But what about objects? Or structure instantiations of a similar function in C. Is there anything wrong with using global objects across multiple source files?

Thanks for sheding some light to this issue as I just got a bit... mangled.

summary

Global scalar type variables are a tool and tools are not good or bad, their use is appropriate or inappropriate. Adding the power of classes to that tool is not good or bad, it has the potential to improve the appropriateness or deteriorate the appropriateness.

Objects

An answer requires a definition of object. Object is not the complement of variable. The complement of variable is constant. I will use "scalar types" as the complement of object.

The problems of using global scalar type variables

It seems that the problems of global variables are in general something along

  • non-locality
  • no constraint checking
  • coupling
  • concurrency problems
  • namespace pollution
  • testing and debugging

I think as long as we talk about scalar types memory footprint is an argument to be considered obsolete at least for PCs, embedded might be a different thing.

On remark: constants do not have all of this problems, thats why global constants are far more common than global variables.

The problems of using global objects

You have to ask yourself whether any of this problems is eliminated by using objects instead of scalar type variables

All of those problems apply to objects as well.

Plus they add all the complexities of classes on top and make every single of those objections worse, at least in general.

With objects you just have more locations to break everything, just some locations more that hinder debugging by side effects. Objects have the tendency to become a little bit more complex, adding to potential consistent initialization problems.

With objects you suddenly not only have to manage access or not, but you have to make sure that every single point of access is compatible to the current interface of your object and the constraints of the underlying data type. Make sure that parts of the program manage ownership correctly.

I think it is not a matter of opinion: If you consider global scalar type variables to be bad you will have to consider global objects to be bad a fortiori.

cum grano salis

Of course there are rightful and perfectly justified applications of global scalar type variables as well as of global objects.

And if several global variables are logically connected I think bundling those connections in a global object might tackle some of the objections. Eg namespace pollution is of course reduced when you move some global scalar type variables in an object. And if there is a invariant in these variables of course the class of a global object is the first place you would be looking for that and a class would definitely the best place to code that invariant. Setters might be a tool to soothe the problem of missing constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM