在C ++中声明/定义类范围常量的位置? [英] Where to declare/define class scope constants in C++?

查看:242
本文介绍了在C ++中声明/定义类范围常量的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇C ++中不同常量声明和定义选项的优点/缺点。最长的时间,我刚刚在头文件的顶部声明他们在类定义之前:

I'm curious about the benefits/detriments of different constant declaration and definition options in C++. For the longest time, I've just been declaring them at the top of the header file before the class definition:

//.h
const int MyConst = 10;
const string MyStrConst = "String";
class MyClass {
...
};

这会污染全局命名空间(我知道这是一件坏事,为什么它是坏的原因列表),常量仍然将范围限定为单个翻译单位,因此不包括此标题的文件将无法访问这些常量。但是,如果其他类定义了一个相同名称的常量,那么可以得到名称冲突,这可能不是一件坏事,因为它可能是可以重构的区域的一个很好的指示。

While this pollutes the global namespace (which I know is a bad thing, but have never found a laundry list of reasons why it is bad), the constants will still be scoped to individual translation units, so files that don't include this header won't have access to these constants. But you can get name collisions if other classes define a constant of the same name, which is arguably not a bad thing as it may be a good indication of an area that could be refactored.

最近,我决定在类定义本身中声明类特定的常量:

Recently, I decided that it would be better to declare class specific constants inside of the class definition itself:

//.h
class MyClass {
    public:
         static const int MyConst = 10;
...
    private:
         static const string MyStrConst;
...
};
//.cpp
const string MyClass::MyStrConst = "String";

常量的可见性将根据常量仅在内部使用类进行调整,是需要使用类的其他对象。这是我认为是最好的选择,现在,主要是因为你可以保持内部类常量私有的类和任何其他类使用公共常量将有一个更详细的引用常量的源(例如MyClass: :MyConst)。它也不会污染全局命名空间。虽然它的确有损于在cpp文件中的非整数初始化。

The visibility of the constant would be adjusted depending on whether the constant is used only internally to the class or is needed for other objects that use the class. This is what I'm thinking is the best option right now, mainly because you can keep internal class constants private to the class and any other classes using the public constants would have a more detailed reference to the source of the constant (e.g. MyClass::MyConst). It also won't pollute the global namespace. Though it does have the detriment of requiring non-integral initialization in the cpp file.

我也考虑将常量移动到自己的头文件,并将它们包装

I've also considered moving the constants into their own header file and wrapping them in a namespace in case some other class needs the constants, but not the whole class definition.

只是寻找意见和其他我还没有考虑过的选项。

Just looking for opinions and possibly other options I hadn't considered yet.

推荐答案

您声称将非整数常量声明为静态类成员会损害在cpp文件中要求非整数初始化不完全固定,所以说。它需要在cpp文件中的定义,但它不是一个损害,这是一个你的意图。 C ++中的命名空间级对象在默认情况下具有内部链接,这意味着在原始变体中,声明

Your claim that declaring a non-integral constant as a static class member "have the detriment of requiring non-integral initialization in the cpp file" is not exactly solid, so to say. It does require a definition in cpp file, but it is not a "detriment", it is a matter of your intent. Namespace-level const object in C++ has internal linkage by default, meaning that in your original variant the declaration

const string MyStrConst = "String"; 

相当于

static const string MyStrConst = "String"; 

它将在包含该头文件的每个转换单元中定义一个独立的 MyStrConst 对象。你知道吗?这是你的意图吗?

i.e. it will define an independent MyStrConst object in every translation unit into which this header file is included. Are you aware of this? Was this your intent or not?

在任何情况下,如果您不需要在每个翻译单元中特别 需要单独的对象, MyStrConst 常量在你的原始示例不是一个好的做法。通常,您只需在头文件中放置一个非定义的声明

In any case, if you don't specifically need a separate object in every translation unit, the declaration of MyStrConst constant in your original example is not a good practice. Normally, you'd only put a non-defining declaration in the header file

extern const string MyStrConst; 

并在cpp文件中提供定义

and provide a definition in the cpp file

const string MyStrConst = "String";

从而确保整个程序使用相同的常量对象。换句话说,当涉及非整数常量时,一个通常的做法是在cpp文件中定义它们。所以,不管你如何声明它(在类或输出),你通常总是必须处理的损害必须在cpp文件中定义它。当然,正如我上面所说的,使用命名空间常量,你可以摆脱你在第一个变种,但这只是一个惰性编码的例子。

thus making sure that the entire program uses the same constant object. In other words, when it comes to non-integral constants, a normal practice is to define them in cpp file. So, regardless of how you declare it (in the class or out) you will normally always have to deal with the "detriment" of having to define it in cpp file. Of course, as I said above, with namespace constants you can get away with what you have in your first variant, but that would be just an example of "lazy coding".

无论如何,我不认为有一个理由使问题过于复杂:如果常数有明显的附件类,它应该声明为类成员。

Anyway, I don't think there is a reason to over-complicate the issue: if the constant has an obvious "attachment" to the class, it should be declared as a class member.

PS访问说明符( public protected private t控制名称的可见性 。他们只能控制其辅助功能。在任何情况下,该名称仍然可见。

P.S. Access specifiers (public, protected, private) don't control visibility of the name. They only control its accessibility. The name remains visible in any case.

这篇关于在C ++中声明/定义类范围常量的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆