Where should you put global constants in a C++ program?

Where would you put global constants in a C++ application? For example would you put them in a class? In a struct?

Where to put global domain specific constants (and how)?

I have a C++ code that’s a physics simulation tool. I would like to store some physical constants, conversion factor between different sets of units, and also some more application specific constants

Where to define global constants in RubyMotion?

I’d like to define global constants in RubyMotion. What is the best way to declare global constants? If it’s Rails, put global constants in config/initializers/constants.rb is common way, I think. Any

Where do I properly put my constants in Meteor

I usually follow the unofficial Meteor FAQ on how to structure my codebase, but I can’t figure out where I should put my global constants. To give an example: I have some database entries with a const

C#: How do you manage symbolic constants in your projects? Where do you declare solution scope constants?

How do you manage symbolic constants in your projects? Where do you declare solution scope constants ?

In Code Igniter, where should I declare my global variables?

I’m new to code igniter. I want to declare some global variables and global constants. Normally, I would put them in the includes/global.php of my own custom framework. Where should I define globals

Where should I put view related constants – backbone.js

Imagine that I have a view, and as part of a view it render ‘x’ model objects, and ‘x’ only. The question, where is it appropriate for me to put this view related constant? My guess would be to do som

Where do you put global variables in a WPF application?

In PHP web programming, I always made a singleton Output object in which I could put global information that had to be available from any line of code. So in WPF applications, I create the following c

Where should constants for events go in a C# project

I’m extremely new to C# and I had a question of convention: Where should constants that are associated with an event be stored? Should they be included in the same place that I define my EventArgs? As

Objective C global constants with case/switch

Is there any way to use global int constants in Objective C that work in a case/switch statement? The technique here (http://stackoverflow.com/questions/538996/constants-in-objective-c) lets me access

Global constants in C++11

What are the best ways to declare and define global constants in C++? I am mostly interested in C++11 standard as it fixes a lot in this regard. [EDIT (clarification)]: in this question global consta


I would use a namespace for global constants which are not very strongly associated with a single class. In the latter case, I would put them in that class.

Really global (application-level) constants should be in the application’s namespace (provided your application is inside it’s own namespace, as it should be). For module-level constants, the module’s own namespace is the natural place.

The best approach is to place them into the same namespace where they belong. A large C++ application will typically have many modules, each with its own set of constants. Each module should be in a namespace, with its constants in it as well.

One option is putting them into a class as non-static members and putting an object of that type inside your main() function. That simplifies making them non-globals when you find out that you need to, for whatever the reason.

As a simple rule of thumb, I put them in whatever place results in them not having irritating C-style prefixes.

So for instance if your constants are named mylib_foo, mylib_bar, then they should be in namespace mylib and be mylib::foo and mylib::bar.

If namespace mylib contains constants circle_max_radius, sphere_max_radius, rectangle_max_width, rectangle_max_height, where Circle, Sphere and Rectangle are classes, then they should static class members.

max is not an “irritating” prefix, it’s quite a sensible one since it’s a descriptive modifier rather than a possessive (“max radius of a circle”). So it can stay.

One possible way is ues a class with static member functions that return the constants. I use this when I need constants that are more than basic types or simple objects.

class Constant
     static const ComplexObject& getComplexObject()
          static ComplexObject constObj = createComplexObject();
          return constObj;
     static ComplexObject createComplexObject()
         ComplexObject obj;
         return obj;

For constants local to a single .cpp file, the best approach is to declare them in an anonymous namespace like so:

namespace {
   int const seconds_per_minute = 60;
   int const minutes_per_hour   = 60;

For constants that need to be visible to the entire program, the simplest solution is to create a (named) namspace for them, and put that in a header file.

If there is (or might be) some operations to go with them, instead you should probably create a singleton class for them. For example, it is quite common that my programs have a singleton class named “configuration” that reads startup constants (stuff like IP addresses, and things I don’t quite want to hard-code) from a config file or the registry or something like that.