我在这里:
class X {
public:
static int shared_arr[];
static void alloc_and_init() {
// Since any static variables defined in the function are allocated some space.
// So can I define X::shared_arr here (using the space the static variable for X::shared_arr)?
// I think it would be a convenient way to make an approach of some basic memory allocation and initialization.
}
};
答案 0 :(得分:2)
不,你必须在一个cpp文件中定义它&在任何功能之外。
int X::shared_arr[MAX_SIZE] = {0};
^^^
请注意,您缺少数组类型。