In the book I am reading at the moment (C++ Complete Reference from Herbert Schildt), it says that no array allocated using new can have an initializer.
Can’t I initialize a dynamically allocated array using new? If not whats the reason for it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s not quite true (you should almost certainly get yourself an alternative reference), you are allowed an empty initializer (
()) which will value-initialize the array but yes, you can’t initialize array elements individually when using array new. (See ISO/IEC 14882:2003 5.3.4 [expr.new] / 15)E.g.
There’s no fundamental reason not to allow a more complicated initializer it’s just that C++03 didn’t have a grammar construct for it. In the next version of C++ you will be able to do something like this.