Why is C++ created in such a way, that if you have a class A and you declare an array of type A, then the whole array gets filled with objects instantiated with the default constructor of the class?
Share
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.
Because when you create an array of a given size, each element of the array has to be valid as soon as it’s created.
If you want a different behavior you can use
vectorandpush_back. A vector is created empty; when you want to add a new element, push_back will take an object that is created any way you want and make a copy of it in the vector.