Suppose that I have a class like this one:
class SpinSystem{
private:
int L;
int L2;
const int k=1, J=1;
const int teq, tcorr;
const double TC=2/log(1+sqrt(2.0));//TC=2.269;
double Temp;
int Nmuestras;
int s[L][L]; int E,M;
int Cluster[L2][2]; int write;
public:
void set_values(int, double, int);
};
void SpinSystem::set_values(int length, double temperature, int nsamples){
L = length;
Temp = temperature;
Nmuestras = nsamples;
L2 = length*length;
teq = (int)(10*pow(length/8.0,0.58));
tcorr = (int)(1*pow(length/8.0,0.58));
}
The private variables L2, teq, tcorr depend on the value of L. This is why I set their values with the method set_values. However, I have to define some arrays, such as s[L][L] and Cluster[L2][2], whose sizes clearly depend on L. How can I achieve this? Thanks in advance.
You should handle all this in a constructor; init functions have all kinds of associated problems. And you should ensure that variables are listed in dependency order (i.e. if one variable depends on another, the latter must be declared first). Also, fixed constants can simply be made static as they are always the same. You don't need to keep variables that are trivially derived from others like L2. Initialize things inline when you can.
class SpinSystem{
private:
static constexpr int k=1;
static constexpr int J=1;
static constexpr double TC=2/log(1+sqrt(2.0));//TC=2.269;
int teq = (int)(10*pow(length/8.0,0.58));
int tcorr = (int)(1*pow(length/8.0,0.58));
int L;
double Temp;
int Nmuestras;
std::vector<int> s;
int E;
int M;
std::vector<int> Cluster;
int write;
public:
void SpinSystem(int length, double temp, int nsamples)
: L(length)
, Temp(temp)
, NMmuestras(nsamples)
{
s.resize(L*L);
Cluster.resize(L*L*2);
}
};
Here I create one dimensional arrays of the same total size. I strongly recommend you just do that and use a simple function to convert the index as you need to. But if you don't like that you can always have nested vectors.
Make sure in your real code to initialize E, M, and Write... preferably inline or in the initializer list.