开发者

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

开发者 https://www.devze.com 2022-12-21 21:12 出处:网络
Where would you put gl开发者_开发知识库obal constants in a C++ application?For example would you put them in a class?In a struct?I would use a namespace for global constants which are not very strongl

Where would you put gl开发者_开发知识库obal constants in a C++ application? For example would you put them in a class? In a struct?


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号