I'd like to be able to "somehow" create a dynamic array that would still let me build its elements using the new operator. Here's what I'd like to achieve:
A* a = new A[3]; //or any equivalent syntax
new (&a[0]) A(myparams1); //placement new on first slot
new (&a[1]) A(myparams2, ...); //placement new on 2nd slot
new (&a[2]) A(myparams3, ...); //placement new on 3rd slot
delete[] a; //correct deletion of all elements
I'm aware that this code would work (minus the 3 elements overwritten), but I'd like to avoid creating 3 default a
elements in the first call to new[]
. I am assuming here that I will always place 3 elements before calling delete[]
.
I'm thinking such a thing can be achieved by using intelligent calls to A::operator new[]
but I'm not sure how.
Does anyone have any idea? it's mostly for curiosity