如何在函数中定义类的静态成员?

时间:2012-03-19 14:19:04

标签: c++ static-methods static-members

我在这里:

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.
    }

};

1 个答案:

答案 0 :(得分:2)

不,你必须在一个cpp文件中定义它&在任何功能之外。

int X::shared_arr[MAX_SIZE] = {0};
^^^

请注意,您缺少数组类型。