I have a hierarchy of classes. The base class uses some tuning parameters that are loadable from file (and reloadable during runtime). Each derived class may add some additional parameters. I am looking for a way to allocate a correctly sized parameters array in the base constructor, so that I don't have to deallocate and reallocate in the derived class. I was hoping for something like this, but it's not working (parameters always has 2 elements):
class Base
{ static int nParms;
virtual int getNParms() { return nParms;}
float *parameters;
public:
Base()
{ parameters= new float[this->getNParms()];}
parameters[0] = globalReloadableX;
parameters[1] = globalReloadableY;
}
};
int Base::nParams =2;
class Derived : public Base
{ static int nParms;
virtual int getNParms() { return nParms;}
public:
Derived() : Base()
{ parameters[2] = globalReloadableZ;
}
}
int Derived::nParams =3;
I've seen this question, but the solution there doesn't quite work for me. I also tried making parameters a regular array in each class:
class Base
{ float parameters[2]
...
class Derived : public Base
{ float parameters[3]
...
but that makes Derived have 2 separate arrays.
Any ideas?