I need to create a dynamically-allocated array of const objects. What makes it difficult is that I need to have values assigned to the const objects too.
I need this for Samples variable of this SFML class.
How should I do it?
I need to create a dynamically-allocated array of const objects. What makes it difficult is that I need to have values assigned to the const objects too.
I need this for Samples variable of this SFML class.
How should I do it?
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()};
Easy:
// Step 1: Make an array of const values:
const int arr[] = { 1, 4, 9, 17 };
// Step 2: Make a pointer to it:
auto parr = &arr; // 2011-style
const int (*pbrr)[4] = &arr; // old-style
You cannot "assign" values to constants (obviously), so the only way to endow a constant with a value is to initialize it to that value.
*pbrr? Thanks!const int * p = new int[3] { 1, 2, 3 };.Should you need a dynamically allocated array, I recommend using a standard container:
std::vector<Int16> data;
Chunk* c = ...;
data.push_back(...);
c->Samples = &data[0];