I can easily declare and initialize a constant array member in a header file like so:
class MyClass {
public:
const int arr[4] = {1, 2, 3, 4};
}
But when the data is defined by a function, I cannot initialize it in the header:
#include <cmath>
#define BASE 2
class MyClass {
public:
const int arr[4];
for (i=0;i<4;i++) {
arr[i] = pow(BASE, i);
}
}
When I try to initialize the array in the class constructor in the .cpp file, I get the obvious uninitialized member with 'const' type
error, as the array should already be initialized.
How can I initialize a const int
array in the header file with a preprocessor macro and cmath functions?