How to create a dynamically-allocated array of const objects, but have values assigned to them?

You don’t need an array of const objects. A pointer-to-const can point to either const or non-const objects; you can create a dynamic array and initialise a Chunk structure from it like this:

std::vector<Int16> samples;
initialise(samples);

// valid until 'samples' is destroyed or resized
SoundStream::Chunk chunk = {&samples[0], samples.size()};

Leave a Comment