FevDeses
FevDeses

Reputation: 183

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?

Upvotes: 17

Views: 5617

Answers (6)

T.E.D.
T.E.D.

Reputation: 44804

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.

Upvotes: 2

Dirk
Dirk

Reputation: 1234

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
{
public:
     static const ComplexObject& getComplexObject()
     {
          static ComplexObject constObj = createComplexObject();
          return constObj;
     }
private:
     static ComplexObject createComplexObject()
     {
         ComplexObject obj;
         obj.setValue(1);
         return obj;
     }
}

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279305

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.

Upvotes: 1

Tronic
Tronic

Reputation: 10430

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.

Upvotes: 0

Péter Török
Péter Török

Reputation: 116276

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.

Upvotes: 14

Eli Bendersky
Eli Bendersky

Reputation: 273616

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.

Upvotes: 7

Related Questions