I have a class with a member array. The length is a constant, but this constant is not known until compile time (In my actual code, this constant is defined differently for different compilation targets). The type of the array is a class with no default constructor.
#define CONSTANT 2
class Data {
public:
Data(int number){}
};
class DemoClass {
private:
Data _member[CONSTANT];
public:
DemoClass():
_member{
Data(0),
Data(0)
}
{
// stuff
}
};
In this example, I can set _member
using the initializer list. However, if the value of COSNTANT
changes, I have to change that initializer list.
In theory, changing DemoClass
to have a default constructor that calls the other constructor with an argument of 0
would work for my case, because I will always call the Data
constructor with 0
. However, I cannot change DemoClass
because it is in an external library.
One solution I've considered is creating the following class:
class CustomData : public Data {
public:
CustomData() : Data(0){}
};
This works, but it seems a bit complicated. Is there a simpler way to initialize this array?