简体   繁体   English

静态常量数组

[英]static const arrays

If I read this in a class as a data member, is the static referring to the entire expression (the array itself) or the elements of the array? 如果我在类中将其作为数据成员读取,则static引用的是整个表达式(数组本身)还是数组的元素?

static const int* array[100];

Is the array, array , static, or does the array contain 100 static const int pointers ? 是array, array ,static还是该数组包含100个static const int pointers

I'm assuming the former, but the way the word const changes meaning makes me wonder if static also changes meaning based on where in the expression it exists. 我假设是前者,但是const单词改变含义的方式使我想知道static也会根据表达式在其中的存在而改变含义。 In the above, the pointer array is not const yet the elements of the array are const , so I wonder if the pointer of the array is static . 在上面,指针array不是constarray的元素 const ,所以我想知道数组的指针是否是static

C++11 7.1.1 Storage class specifiers: C ++ 11 7.1.1存储类说明符:
§5 §5

The static specifier can be applied only to names of variables and functions and to anonymous unions (9.5). 静态说明符只能应用于变量和函数的名称以及匿名联合(9.5)。 There can be no static function declarations within a block, nor any static function parameters. 块中不能有静态函数声明,也不能有任何静态函数参数。 A static specifier used in the declaration of a variable declares the variable to have static storage duration (3.7.1), unless accompanied by the thread_local specifier, which declares the variable to have thread storage duration (3.7.2). 变量声明中使用的静态说明符声明该变量具有静态存储持续时间 (3.7.1),除非带有thread_local说明符伴随它声明该变量具有线程存储持续时间(3.7.2)。 A static specifier can be used in declarations of class members; 静态说明符可用于类成员的声明中。 9.4 describes its effect. 9.4描述其效果。 For the linkage of a name declared with a static specifier, see 3.5. 有关使用静态说明符声明的名称的链接,请参见3.5。

So what are you declaring in the code example? 那么,您在代码示例中声明了什么?

static const int* array[100];

Obviously, you are declaring the variable array So the static applies to array . 显然,您是在声明变量array因此static适用于array
Do not confuse storage class specifier with cv-qualifiers the former applies to the variable being declared while the latter applies to the type . 不要将存储类说明符cv-qualifiers混淆,前者适用于所声明的变量,而后者适用于type

static , extern and register apply to the variable you declare, to array in this example. staticexternregister适用于您声明的变量,在此示例中适用于array

So, you get a static array of 100 pointers to constant integers. 因此,您将获得一个由100个指向常量整数的指针的静态数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM