Why do I have to provide default ctor if I want to create an array of objects of my type?
Thanks for answers
Why do I have to provide default ctor if I want to create an
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 they have to be initialized.
Consider if it wasn’t the case:
Note you don’t have to:
If you really need an array of objects and you can’t give a meaningful default constructor, use
std::vector.If you really need an array of of objects, can’t give a meaningful default constructor, and want to stay on the stack, you need to lazily initialize the objects. I have written such a utility class. (You would use the second version, the first uses dynamic memory allocation.)
For example: