Given a simple class MyClass with a constructor that accepts two int, how can I initialize an array of MyClass in the heap?
I’ve tried
MyClass *classes[2] = { new MyClass(1, 2),
new MyClass(1, 2) };
But this doesn’t seem to work. Thanks
Use the
std::allocator<MyClass>for this.Note that you don’t have to construct all that you allocate.
Or, better yet, just use
std::vector.[EDIT]
KerrekSB suggested this as simpler:
It’s slightly slower access, but much easier to use.