常量的 C++ 最佳实践

C++ Best practices for constants(常量的 C++ 最佳实践)

本文介绍了常量的 C++ 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆常量,我想在代码的不同部分访问它们,但我希望能够轻松访问它们:

I have a whole bunch of constants that I want access to in different parts of my code, but that I want to have easy access to as a whole:

static const bool doX = true;
static const bool doY = false;
static const int maxNumX = 5;

等等.

所以我创建了一个名为constants.h"的文件,并将它们全部粘贴在那里,并将其#included 到任何需要知道常量的文件中.

So I created a file called "constants.h" and stuck them all in there and #included it in any file that needs to know a constant.

问题是,这对于编译时间来说很糟糕,因为每次我更改一个常量时,都必须重建 constants.h 引用的所有文件.(另外,据我了解,因为它们是静态的,所以每次我在新的 .cpp 中包含 constants.h 时,我都会在代码中生成一份 doX/doY/maxNumX 的副本,从而导致编译后的千字节浪费空间EXE——有什么办法可以看到吗?).

Problem is, this is terrible for compile times, since every time I change a constant, all files that constants.h reference have to be rebuilt. (Also, as I understand it, since they're static, I'm generating a copy of doX/doY/maxNumX in code every time I include constants.h in a new .cpp, leading to kilobytes of wasted space in the compiled EXE -- is there any way to see this?).

所以,我想要一个解决方案.如果可能的话,不是仅在使用它们的文件中声明常量".

So, I want a solution. One that isn't "declare constants only in the files that use them", if possible.

有什么建议吗?

推荐答案

我认为你的基本假设不成立.

I think your base assumption is off.

您的其他标题通常是通过将协同工作的内容放在一起来组织的.例如,一个类及其相关方法或两个高度相互关联的类.

Your other headers are usually organized by keeping together what works together. For example, a class and its related methods or two classes heavily interlinked.

为什么将所有常量组合在一个标题中?它没有任何意义.像 "global.h" 标头轻松包含每个依赖项一样糟糕.

Why group all constants in a single header ? It does not make sense. It's about as bad an idea as a "global.h" header to include every single dependency easily.

通常,常量用于特定的上下文.例如,用作特定函数标志的枚举:

In general, the constants are used in a particular context. For example, an enum used as a flag for a particular function:

class File {
public:
  enum class Mode {
    Read,
    Write,
    Append
  };

  File(std::string const& filename, Mode mode);

  // ...
};

在这种情况下,这些常量与它们绑定到的类(甚至在类中)位于同一个标头中是很自然的.

In this case, it is only natural that those constants live in the same header that the class they are bound to (and even within the class).

另一类常量是那些渗透到整个应用程序的常量.例如:

The other category of constants are those that just permeate the whole application. For example:

enum class Direction {
  Up,
  Down,
  Right,
  Left,
  Forward,
  Backward
};

...在一个游戏中,您想表达物体所面对方向的移动.

... in a game where you want to express objects' move regarding the direction they are facing.

在这种情况下,为这组特定的常量创建一个头文件就可以了.

In this case, creating one header file for this specific set of constants is fine.

如果您真的担心将这些文件组合在一起:

And if you really are worried about grouping those files together:

constants/
  Direction.hpp
  Sandwich.hpp
  State.hpp

当你添加一个常量时,你会巧妙地回避重新编译整个应用程序的问题......虽然如果你需要这样做,你只需支付一次成本,比你错误的设计更好'余下的工作都得靠你过活.

And you will neatly sidestep the issue of recompiling the whole application when you add a constant... though if you need to, do it, you're paying the cost only once, better than a wrong-sided design you'll have to live off with for the rest of your work.

这篇关于常量的 C++ 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:常量的 C++ 最佳实践

基础教程推荐