我想在其源文件中将常量数组定义为类的数据成员。
数组有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,
};
};
我的问题:
1-将数组的定义转移到源文件是否更好?
2-无论如何在源文件中定义此数组而不是定义类的头?
答案 0 :(得分:3)
:
static const int myArray[256];
在cpp文件中
const int Foo::myArray[256] = {
// numbers
};
我假设你不需要每个班级实例。
关于哪个地方更好的问题;我会把它放在cpp文件中,但这主要是风格问题。