OOPortal OOPortal


Constructor Functions  «Prev  Next»
Lesson 8 The default constructor
Objective Get familiar with creating default constructors.

Default Constructor in C++

A constructor with a void argument list or a list whose arguments all have defaults is called the default constructor. A default constructor has the special purpose of initializing arrays of objects of its class. It is often convenient to overload the constructor with several function declarations. In our mod_int example from the last lesson, it could be desirable to have the default value of v be 0. By adding the default constructor

mod_int() { v = 0; }

as a member function of mod_int, it is possible to have the following declarations:
mod_int  s1, s2;  // init private member v to 0
mod_int  d[5];    // arrays properly initialized

In both of these declarations, the empty parameter list constructor is invoked.
If a class does not have a constructor, the system provides a default constructor. If a class has constructors, but does not have a default constructor, then array allocation causes a syntactic error.

Default constructors

A default constructor is one that can be called with no arguments. A default constructor is used to create a "generic object", but it is also important when the compiler is told to create an object but is not given any details. For example, if you take the struct Y defined previously and use it in a definition like this,
Y y2[2] = { Y(1) };
the compiler will complain that it cannot find a default constructor. The second object in the array wants to be created with no arguments, and that is where the compiler looks for a default constructor. In fact, if you simply define an array of Y objects,
Y y3[7];
the compiler will complain because it must have a default constructor to initialize every object in the array.
The same problem occurs if you create an individual object like this:
Y y4;
Remember, if you have a constructor, the compiler ensures that construction always happens, regardless of the situation. The default constructor is so important that if (and only if) there are no constructors for a structure (struct or class), the compiler will automatically create one for you. So this works:
// Automatically-generated default constructor
class V {
int i; // private
}; // No constructor
int main() {
V v, v2[10];
} ///:~
If any constructors are defined, however, and there is no default constructor, the instances of V above will generate compile-time errors.
You might think that the compiler-synthesized constructor should do some intelligent initialization, like setting all the memory for the object to zero. But it does not, since that would add extra overhead but be out of the programmer's control.
If you want the memory to be initialized to zero, you must do it yourself by writing the default constructor explicitly. Although the compiler will create a default constructor for you, the behavior of the compiler-synthesized constructor is rarely what you want. You should treat this feature as a safety net, but use it sparingly. In general, you should define your constructors explicitly and not allow the compiler to do it for you.