Reputation: 2281
I have a C++ application where I want to use an array that is initialized on its declaration. The problem, though, is that the number of items is not fixed in the "normal" way, but it's based on a "counter" number inside a enum:
//"Normal way"
void myMethod()
{
bool myArray[3] = { false, false, false };
//...
}
//My way
//In .hpp
enum MyEnum
{
Item1,
Item2,
...,
MyEnumCount
}
//In .cpp
void myMethod()
{
bool myArray[MyEnumCount] = { false, false, false };
//...
}
One situation that might occur while developing is a change in the MyEnum
definition, either by increasing or decreasing the number of items on it. The idea is that, in case this happens, the code would automatically adapt itself to the new enum, without having to manually go to parts of the code for specific editions. Is there a way to do this in that array initialization? Or in this specific case, I would have to manually change its initialization always when MyEnum is changed?
Edit: Thanks for the all the replies written so far. I'm editing my question because I think my example above mislead some users in providing an answer for the actual question that was made (which is, actually, slightly different from the code I want for my app).
Restating my question: how does one initialize an array on its declaration with the values he wants when the size of the array is determined by a enum value which may be edited during code development without having to re-write the initialization?
This means:
static
one, the question is actually universal, that is, independent on the array being static
or not. So no reply that takes advantage of a array being static
is valid. Of course that not being a static
array would left opened the question about why not use a for loop or similar to to the initialization, but in that case the answer should only do a "switch case": "if you have such an array, the way of doing this is the fallowing; if you have another kind of array, then the way is this other".Since I made this edits, I'll wait till tomorrow for a complete answer and, if not, I'll made one with the answers you provided. Thanks!
Upvotes: 1
Views: 1572
Reputation: 310980
It is simple to do by specifying an empty brace-init list
void myMethod()
{
static bool myArray[MyEnumCount] = {};
//...
}
In this case all elements of the array will be initialized to false
. As the array is static then you may even do not specify the initializer. It will be zero-initialized by the compiler itself.
Take into account that you could use std::array
:
For example
void myMethod()
{
static array<bool, MyEnumCount> myArray = {};
//...
}
In this case you could reassign the array using the brace-init list.
Upvotes: 4
Reputation: 1047
Do you have to use arrays? or can you use standard containers? For example, with vectors this can be easily done:
static std::vector<bool> myArray(false, MyEnumCount);
but there might be some problems with this being a static array. You need to make sure when the array is being initialized that the variable MyEnumCount
has been already defined. In your case, because the static array is inside a function, you just need to make sure it is called after the definition of the count, which probably is.
Upvotes: 1
Reputation: 281505
It's fine to use an initializer that doesn't specify enough values, and the remaining values will take the default for that type:
enum MyEnum
{
Item1,
Item2,
Item3,
MyEnumCount
};
//In .cpp
void myMethod()
{
bool myArray[MyEnumCount] = {false, true}; // The third will be false
//...
}
Upvotes: 1
Reputation: 137315
If you want to initialize everything to false, use an empty list:
bool myArray[MyEnumCount] = {};
If you want to initialize everything to something else, use std::fill
or std::fill_n
:
bool myArray[MyEnumCount];
std::fill_n(myArray, MyEnumCount, true);
// or std::fill(myArray, myArray + MyEnumCount, true);
Upvotes: 2
Reputation: 30605
The code you have there (with MyEnumCount
) will work since the MyEnumCount
is constant at compile time.
With respect to the initialisation, there are some options available to you;
static bool myArray[MyEnumCount] = {};
Or (if the value is something more complex
static bool myArray[MyEnumCount];
// some place safe...
for (auto it = std::begin(myArray); it != std::end(myArray), ++it) {
*it = false;
}
Upvotes: 2
Reputation: 385897
If it wasn't static,
bool myArray[MyEnumCount];
for (int i=MyEnumCount; i--; )
myArray[i] = false;
Upvotes: 1