Jua*_*esa 13 c++ arrays const declaration
我有一个类,我想要一些值为0,1,3,7,15的位掩码,......
所以基本上我想声明一个常量int的数组,例如:
class A{
const int masks[] = {0,1,3,5,7,....}
}
Run Code Online (Sandbox Code Playgroud)
但编译器总会抱怨.
我试过了:
static const int masks[] = {0,1...}
static const int masks[9]; // then initializing inside the constructor
Run Code Online (Sandbox Code Playgroud)
有关如何做到这一点的任何想法?
谢谢!
Joh*_*itb 24
class A {
static const int masks[];
};
const int A::masks[] = { 1, 2, 3, 4, ... };
Run Code Online (Sandbox Code Playgroud)
您可能希望已经在类定义中修复了数组,但您不必这样做.该数组在定义点(将保留在.cpp文件内,而不是在标题中)具有完整类型,它可以从初始化程序中推断出大小.
// in the .h file
class A {
static int const masks[];
};
// in the .cpp file
int const A::masks[] = {0,1,3,5,7};
Run Code Online (Sandbox Code Playgroud)