Why can't I call a parameterized constructor with new in c++?
If you know the issue,
Assume I have a class A whose CTOR receives an integer;
I cannot do the following :
A* arr = new A[3](A(2), A(3), A(5));
Or any other way to initialize several members of an array. I read around, its just not possible.
My question is why, why can I do
A arr[3] = {A(1), A(2), A(3)};
but I can not do the above? Memory-wise or whatever.开发者_StackOverflow社区
Thank you very much!
The reason you can't do this in current standard C++ (referred to as C++03) is historical. This will be cleaned up in the upcoming C++ standard (currently expected to be released this year, which would make it C++11), which will introduce what's called "uniform initialization syntax".
According to Stroustrup's C++0x FAQ, you can then write
A* p = new A[3] {A(1), A(2), A(3)};
There's a pretty good chance your compiler is actually already supporting this.
I believe you are trying to do "uniform initialization" which is included in C++0x. I don't get why the initialization list uses A(int)
parts, I'd just do this (which is accepted by C++98/03:
A arr[3] = {1, 2, 3};
精彩评论