Qiangzini
Qiangzini

Reputation: 537

Alias for array elements in c/c++

I have a system with more than 40 ODEs to solve, which normally can be done with some library like intel ODE. Due to the nature of this problem, these libraries only take arrays as parameter. In this way I have to put all the states of the ODEs into one array, say Y[0], ... , Y[39]. Clearly as it is, this makes the whole program painful to read and write. I have to check what Y[i] denotes every time!

So, is there any good way to use alias for these arrays? Also, I am thinking about putting all the system into a class, which makes using reference to the array elements more difficult.

Upvotes: 0

Views: 2863

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

You could use an enum for indexes into Y:

enum Quantities {
  kDistance,
  kVelocity,
  kAcceleration,
  ...
};

Now you can write Y[kDistance] everywhere in place of Y[0].

Or, you could define named references for each array element:

double Y[40];
double& Distance = Y[0];
double& Velocity = Y[1];
...

Now you can write Distance everywhere in place of Y[0].

Upvotes: 3

Related Questions