I want to initialize a constant in the child class instead of the base class. And use it to get rid of dynamic memory allocation (I already know the size of the array, and there will be several child classes with different constants).
Therefore, I try:
class A {
public:
const int x;
A() : x(0) {}
A(int x) : x(x) {}
void f() {
double y[this->x];
}
};
class B : A {
B() : A(2) {}
};
Pretty simple, but the compiler says:
error C2057: expected constant expression
How can I tell the compiler that it really is a constant?
source
share