I\'m creating a custom vector class for a school project and I\'d like to be able to initialize it like this:
vector x = { 2, 3, 4, 5 };
Is the
You can support that by adding a constructor that takes a std::initialzer_list<double>
.
vector(std::initializer_list<double> init) : vsize(init.size()),
valloc(init.size()),
values(new double[init.size()])
{
std::copy(init.begin(), init.end(), values);
}
You can make that a bit more flexible by using a template.
template <typename T>
vector(std::initializer_list<T> init) : vsize(init.size()),
valloc(init.size()),
values(new double[init.size()])
{
std::copy(init.begin(), init.end(), values);
}