[英]Define const array in source file
I want to define a constant array as data member of class in its source file. 我想在其源文件中将常量数组定义为类的数据成员。
The array has 256 elements, and there is no relation between them. 该数组有256个元素,它们之间没有关系。
class Foo {
private:
const int myArray[256] = {
0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0,
0, 0, 3, 0, 3, 0, 0, 1, 3, 0, 0, 0, 0, 1, 2, 0,
0, 0, 2, 2, 1, 0, 3, 2, 0, 2, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 0,
0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
};
My Questions: 我的问题:
1- Is it better to shift the definition of the array to the source file? 1-将数组的定义转移到源文件是否更好?
2- Is there anyway to define this array in a source file instead of the header that defines the class? 2-反正有没有在源文件中定义此数组,而不是定义类的标头?
in the header: 在标题中:
static const int myArray[256];
in the cpp file 在cpp文件中
const int Foo::myArray[256] = {
// numbers
};
I'm assuming here you don't need one per class instance. 我假设这里每个类实例不需要一个。
Regarding the question of where it's better to put it; 关于最好放在哪里的问题; I would put it in the cpp file, but that's mostly a matter of style.
我会将其放在cpp文件中,但这主要是样式问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.