I have some code like this:
class MyObject {
private:
int x;
public:
MyObject(int x) : x(x) {}
};
I want to initialize 5 MyObject instances. I know I can do this with g++ 8.3.0.
MyObject obj_array[5]{1, 2, 3, 4, 5};
However, I'm unable to do this:
const int myInts[5] = {1, 2, 3, 4, 5};
MyObject obj_array[5](myInts);
Is there any way to make the second initialization method (initializing array of objects with initialization list constructors using const integer array) work? The rub is that we have a special compiler framework that doesn't allow dynamic memory or most STL datatypes such as vector.
If we're doing ugly solutions, here's one with scalability:
#define LIST { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
const int myInts[] = LIST;
MyObject obj_array[] = LIST;
#undef LIST
Not pretty, but this seems to work:
const int myInts[5] = { 1, 2, 3, 4, 5 };
MyObject obj_array[5] = {myInts[0], myInts[1], myInts[2], myInts[3], myInts[4]};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With