Determinant
Determinant

Reputation: 4056

How to define a static member of a class in the function?

Here I have:

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

};

Upvotes: 0

Views: 81

Answers (1)

Alok Save
Alok Save

Reputation: 206646

No, You will have to define it in exactly one cpp file & outside of any function.

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

Note that you are missing the array type.

Upvotes: 2

Related Questions