Re: How to Call "Constructor with parameter" after new [] allocation?
On Sep 23, 9:27 am, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
[...]
So, when allocating an array T[n], only these two syntaxes are legal:
new T[n]; // #1 new-initializer omitted
new T[n](); // #2 new-initializer is ()
there is no third case.
Well, we can add the third case using placement new:
T mem[N]; // or dynamical allcation
T* arr = new (mem) T[n];
Although I do not think it is a good method, but if
the pre-allocated memory can be initialized and the arr
can just use the initialized value in my mind.
On the other hand, new-expressions to allocate arrays is a very
error-prone technique that should be strongly discouraged except when
programming at a very low-level. It's much better to use a container and
std::vector in particular.
which is quite true in my mind.
If heap allocation is not an issue and the array size can
be estimated then std::tr1::array is another choice:
std::tr1::array<int,3> arr = {{1,2,3}};
Since the array class is an aggregate, we can specify different
values in the initializer.
Ganesh
Jiang
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]