user188276
user188276

Reputation:

what is the best way to use global variables in c++?

For me, I usually make a global class with all members static. All other classes will inherit from this global class.

I wonder if this is a good practice?

Anybody got any suggestions?

Upvotes: 3

Views: 2667

Answers (7)

Matthieu M.
Matthieu M.

Reputation: 300349

There are 2 things that chagrined me: The use of global variables is bad, but sometimes it's difficult to do without, however I am chagrined by:

  • The clear abuse of inheritance
  • The wonderful dependency issue

The combination of the two has a staggering effect.

Let's say I create a class that will access a global variable, from your Anti-Pattern it gives:

#include "globals.h"

class MyClass: Globals // for my own sake I assume it's not public inheritance
{
};

Of course, the #include is mandatory in the header, since I inherit from it. Therefore each time I add / change one of the global, even one that is used by a single class... I recompile the whole application.

If we'd ever worked on the same team, that would earn you a very harsh, very stern comment... to say the least.

Upvotes: 1

Chad Simpkins
Chad Simpkins

Reputation: 121

You are most likely looking for the Singleton pattern. That is not to say that all global variables need to use the pattern. But, when I have a global it is usually because I want only one instantiation for the entire program. In this case, singleton can work very well.

http://en.wikipedia.org/wiki/Singleton_pattern

Upvotes: -1

David Thornley
David Thornley

Reputation: 57066

First, global state is bad. It seriously complicates understanding the program, since the behavior of any part can depend on the global variables. It makes it harder to test. It provides a way by which two far-distant functions can create an inconsistent state that may mess up another function, and that will be very difficult indeed to debug.

The nature of the global state doesn't matter. This is what's usually maligned about the Singleton pattern, for example.

However, having every class inherit from one global variable class is a bad idea. In C++, inheritance should be used sparingly, as it ties two classes together in implementation. It's usually a bad thing to have all the classes inheriting from one base class in any form, and C++ doesn't handle multiple inheritance really well. It would be really easy to get the "deadly diamond" effect, since if A inherits from B and they both inherit from Global, Global will appear twice in A's inheritance hierarchy.

Upvotes: 1

Clifford
Clifford

Reputation: 93566

Your suggested practice has not solved a single problem associated with global variables.

  • Private member data with access functions allow single point control of read/write and validation, improving robustness, maintainability, and ease of debugging.
  • Making the data members static simply reduces flexibility; you might want multiple independent global objects containing the same data structure. If there should only ever be one, use the singleton pattern.
  • Collecting unrelated global data into a single class breaks best practice regarding coupling and cohesion and is hardly OO.

This article relates to C and embedded systems, but is no less relevant to your question.

Upvotes: 1

Callie J
Callie J

Reputation: 31326

Best way? Carefully... :-)

Upvotes: 1

James Curran
James Curran

Reputation: 103575

Ick!

A global variable is a global variable. Renaming it -- even with a name that makes it look like a member varaible, doesn't change that. Every problem that you have with a oridinary global variable you will still have with your global-variable-as-common-static-member scheme (and possibly a few new ones).

Upvotes: 0

Brian R. Bondy
Brian R. Bondy

Reputation: 347566

Generally try to avoid global variables as they introduce global state. And with global state you do not have referential transparency. Referential transparency is a good thing and global state is a bad thing. Global state makes unit tests pretty pointless for example.

When you have to though, I'd agree that most of the time the method you mentioned is fine. You can also declare the global variable in any .cpp file and then have in your .h file an extern to that global variable.

Upvotes: 6

Related Questions