Is there a way to statically-initialize a dynamically-allocated array in C++?

You can in C++0x:

int* p = new int[3] { 1, 2, 3 };
...
delete[] p;

But I like vectors better:

std::vector<int> v { 1, 2, 3 };

If you don’t have a C++0x compiler, boost can help you:

#include <boost/assign/list_of.hpp>
using boost::assign::list_of;

vector<int> v = list_of(1)(2)(3);

Leave a Comment