Arrays of constructors and classes for C + + classes

Source: Internet
Author: User

When you create an array of class types, members that are not initialized call the class's default constructor , and other constructors cannot and cannot be called

Therefore, when a class is to be used as an element type of an array, the class must have a default constructor! Otherwise, the array must be explicitly initialized with the specified constructor for each element!

If the base class has a default constructor:

Base array[3]; legal //3 members will call base's default constructor, base does not have default constructor to error ? Base array[5] = {Base (1,2), base (1)}; legal //  The latter 3 members call the default constructor, and base has no default constructor to make an error

If the base class does not have a default constructor, you must:

Base array[5] = {Base (1,2), base (1), base (1), base (1), base ( 1 )}; // Each member cannot be omitted and must be explicitly initialized with the specified constructor

In addition, when the new object array is called, only the default constructor is invoked!

You cannot call a non-default constructor! That is, if the base class has no default constructor, you cannot new its object array!
Like what:

New base[3new base[3] (); // 3 Members will call the default constructor  New base[3] (1,2)// attempting to invoke Base (int) for three members , int), the compiler will error ?

Arrays of constructors and classes for C + + classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.