C++ 静态常量与常量静态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12005959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:45:06  来源:igfitidea点击:

static const versus const static

c++classconst

提问by BigBrownBear00

I do not understand the difference between these two statements in my C++ class:

我不明白我的 C++ 类中这两个语句之间的区别:

class MyClass {
  public:
  private:
     const static int var = 0;            // Option 1
     static const int var = 0;            // Option 2
};

What is the difference b/w Option 1 and Option 2?? They both compile.

黑白选项 1 和选项 2 有什么区别??他们都编译。

回答by

They mean exactly the same thing. You're free to choose whichever you think is easier to read.

它们的意思完全相同。您可以自由选择您认为更容易阅读的内容。

In C, you shouldplace staticat the start, but it's not yet required. I'm not sure if C++ followed C in this regard.

在 C 中,您应该放在static开头,但现在还不是必需的。我不确定 C++ 在这方面是否遵循 C。

6.11.5 Storage-class specifiers

1 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

6.11.5 存储类说明符

1 将存储类说明符放置在声明中声明说明符的开头以外的位置是过时的功能。

回答by James Kanze

static, const(here, anyway) and the type (e.g. int) are all part of the declaration specifier. Historically, the declaration specifier was an unordered list of keywords and type names, so:

static, const(在这里,无论如何)和类型(例如int)都是声明说明符的一部分。 从历史上看,声明说明符是关键字和类型名称的无序列表,因此:

static unsigned int const var;
static unsigned const int var;
static int unsigned const var;
static int const unsigned var;
static const unsigned int var;
static const int unsigned var;
unsigned static int const var;
unsigned static const int var;
unsigned int static const var;
unsigned int const static var;
unsigned const static int var;
unsigned const int static var;
int static unsigned const var;
int static const unsigned var;
int unsigned static const var;
int unsigned const static var;
int const static unsigned var;
int const unsigned static var;
const static unsigned int var;
const static int unsigned var;
const unsigned static int var;
const unsigned int static var;
const int static unsigned var;
const int unsigned static var;

were all legal, and all meant the same thing.

都是合法的,都意味着同样的事情。

I think that this is still the case, both in C and in C++, but if I'm not mistaken, C has deprecated putting the storage class specifier (static) any where but at the beginning. This is at any rate an almost universal convention, so you should normally put the static(and extern, etc.) at the start.

我认为在 C 和 C++ 中仍然是这种情况,但如果我没记错的话,C 已经弃用了将存储类说明符 ( static)放在任何地方,但不是在开头。无论如何,这是一个几乎通用的约定,因此您通常应该将static(和extern等)放在开头。

Note too that being unordered only applies to the declaration specifier. Within the declarators which follow, the cv-qualifier(s) mustfollow what they qualify; for reasons of orthogonality, you should normally always put the cv-qualifiers after what they modify (i.e. int const, and not const int).

另请注意,无序仅适用于声明说明符。在随后的声明符中,cv 限定符必须遵循它们所限定的内容;出于正交性的原因,您通常应该始终将 cv 限定符放在它们修改的内容之后(即int const,而不是const int)。

Finally, it seems to be a widespread convention to present the type modifiers before the type, with the signedness modifier (signedor unsigned) preceding the length modifier (short, longor long long). It's also fairly frequent to drop the intif a modifier is present, so people write unsigned, rather than unsigned int, and long, rather than long int. This is far from universal, however.

最后,它似乎是类型之前呈现型改性剂,与所述符号性改性剂(一种普遍惯例signedunsigned)长度改性前述(shortlonglong long)。删除intif 修饰符也相当频繁,所以人们写unsigned, 而不是unsigned int, and long, 而不是long int。然而,这远非普遍。

Given this, the first way the declaration is written, above, is preferred, although it is quite acceptable to drop the int.

鉴于此,上述声明的第一种编写方式是首选,尽管删除int.

回答by Ed Heal

They are the same. But I would always go for option 2 for a simple reason that the keywords constand intfit better when juxtaposed as they define the datatype. Where as the keyword staticdefines the accessibility of that variable.

他们是一样的。但我总是会选择选项 2,原因很简单,即关键字constint在定义数据类型时并列时更适合。其中关键字static定义了该变量的可访问性。

回答by nutrina