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