0

I have a numerical Vector class, which is a wrapper on a float array, and I am sick of element-by-element initialization:

Vector vec(3);
vec(1) = 1;
vec(2) = 2;
vec(3) = 3;

Without using C++11 (Boost ok, but not preferable), what operators and tricks can I play to do this all at once, something resembling a brace-enclosed initializer list like:

Vector vec(3) = {1,2,3};
// --OR--
Vector vec(3) << 1 << 2 << 3;

or anything sane really.

Philip G. Lee
  • 420
  • 1
  • 3
  • 8

1 Answers1

0

One possibility is to give your Vector class an int* constructor and then initialise it from an int array that you can initialise with a standard initaliser list. So something like this:

int vecData[] = {1,2,3};
Vector vec(vecData, sizeof(vecData)/sizeof(vecData[0]));

It's not the perfect solution, but probably more convenient than initalising elements one by one.

James Holderness
  • 22,721
  • 2
  • 40
  • 52