A
aaragon
Hi, just a very simple question. I was wondering what is the most
efficient way of initializing an array. I have a very simple class
that wraps an array to provide bound checking, something like this:
enum checkMode {CHECK, NOCHECK};
template <int n, checkMode c = CHECK>
struct ArrayStructure {
static const short dim_ = n;
double& operator[](size_t i) {
if(i >= dim_ || i < 0)
throw RuntimeError("*** ERROR *** Array access out of
bounds. \
\nCheck the index when accesing the
fitness or constraint array.");
return storage;
}
double storage[n]; //!< storage for elements
};
Now, I need that the storage[n] be initialized to zeros. Is there a
way to do this without having to write the constructor? If not, and I
have to write it anyways, is there a way to initialize ALL values in
the container in the initialization list? (I believe the
initialization list is always the most efficient way to initialize
variables, to avoid the assignment call). Thank you,
a²
efficient way of initializing an array. I have a very simple class
that wraps an array to provide bound checking, something like this:
enum checkMode {CHECK, NOCHECK};
template <int n, checkMode c = CHECK>
struct ArrayStructure {
static const short dim_ = n;
double& operator[](size_t i) {
if(i >= dim_ || i < 0)
throw RuntimeError("*** ERROR *** Array access out of
bounds. \
\nCheck the index when accesing the
fitness or constraint array.");
return storage;
}
double storage[n]; //!< storage for elements
};
Now, I need that the storage[n] be initialized to zeros. Is there a
way to do this without having to write the constructor? If not, and I
have to write it anyways, is there a way to initialize ALL values in
the container in the initialization list? (I believe the
initialization list is always the most efficient way to initialize
variables, to avoid the assignment call). Thank you,
a²