I was asked to provide solution for the following problems:
There is a structure defining some int parameters:
struct B {
int a;
int b;
};
One wanted to define this structure as const static members in other classes (not only for this class A – there are other classes expected to have the same set of constants) .
And one wanted to use them as real integral constants:
// .h file
class A {
public:
static const B c; // cannot initialize here - this is not integral constant
};
// .cpp file
const B A::c = {1,2};
But cannot use this constant to make for example an array:
float a[A::c.a];
Any advice?
If you make
A::cconstexpryou can initialise it inline and use its members as a constant: