Possible to use NEW and Initializer List on a Struct array

Hi all,

Is it possible to use new and an initialize list on a struct array.

For example in my base class i have a pointer to a struct:

MyStruct* m_struct;

I want to initialize it like this in the derived class:

myStruct = new MyStruct[100] { var1, var2, var3 };

Is it possible or do i need have to make a new instance of the struct array and memcpy it into the pointer?

Thanks.
Neither. Use a std::vector.
We can't use the std lib.
Last edited on
That's good to hear. So why haven't you used a vector in the first place?
We can't use STL it's not available on the target platform

sorry you must have read the previous post before i added "Can't"
Last edited on
I see. However, that doesn't keep you from implementing an equivalent class yourself.
Aside from that, the answer is still: no, you can't do that with new[].
Ok, thanks.
If your compiler supports C++11 uniform initialization syntax:
http://www.devx.com/cplus/10MinuteSolution/39896/1954

1
2
3
4
5
6
7
8
9
10
11
12
struct A
{
    double d ;
    std::string str ;
    int i[4] ;
};

int main()
{
    A* pa = new A[5]{ { 1.2, "abc", { 1, 2, 3 } }, { 3.4, "defgh", { 5, 6, 7, 8 } },
                      { 5.6, "", { 9 },  }, { 7.8, "IJ", { 0 } }, { 0.0, "", {0} } } ;
}
Thanks for the answer JLBorges, that was exactly what i was looking for unfortunately we are using an old compiler, so i can't use it. Def. good to know though.
Topic archived. No new replies allowed.