If I have:
struct a_struct
{
int an_int;
a_struct(int f) : an_int(f) {}
a_struct() : an_int(0) {}
};
class a_class
{
a_struct * my_structs;
a_class() {...}
};
I can do:
a_class() {my_structs = new a_struct(1)}
//or
a_class() {my_structs = new a_struct [10]}
But I cannot do:
a_class() {my_structs = new a_struct(1) [10]}
//or
a_class() {my_structs = new a_struct() [10]}
Is there any correct syntax to get this to work? Or an easy work around?
If using the STL is an option, you could use std::vector instead of a dynamic array.
I think that this will work:
std::vector<a_struct> my_structs;
my_structs.assign(10, 1);
If not, this should:
my_structs.assign(10, a_struct(1));
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