It is unfortunate that C++ doesn't handle that syntax.
First thing I'll note...a "switch" statement implicitly defines a lookup table, and is probably the best way to handle this kind of problem regardless of what the language allows.
Having said that, to do initialization on the fly in C++, one trick you can do is to write a function. The main downside is that you must add a "()" wherever the "variable" is used, instead of just a variable name.
So to continue their example, you might say:
enum {
PL_SIZE = 56
};
static const int *price_lookup() {
static int *_ = NULL;
if (!_) {
_ = new int[PL_SIZE];
_[APPLES] = 6;
_[ORANGES] = 10;
_[STRAWBERRIES] = 55;
}
return _;
}
...and then, e.g. instead of price_lookup[i] you'd have to say price_lookup()[i]. Obviously their ARRSIZE() macro no longer works, either, hence the PL_SIZE definition.
First thing I'll note...a "switch" statement implicitly defines a lookup table, and is probably the best way to handle this kind of problem regardless of what the language allows.
Having said that, to do initialization on the fly in C++, one trick you can do is to write a function. The main downside is that you must add a "()" wherever the "variable" is used, instead of just a variable name.
So to continue their example, you might say:
...and then, e.g. instead of price_lookup[i] you'd have to say price_lookup()[i]. Obviously their ARRSIZE() macro no longer works, either, hence the PL_SIZE definition.