- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 522字
- 2021-06-10 18:28:13
Defining constructors
The default constructor is used when an object is created without a value and hence the object will have to be initialized with a default value. The point declared previously could be implemented like this:
class point
{
double x; double y;
public:
point() { x = 0; y = 0; }
};
This explicitly initializes the items to a value of zero. If you want to create an instance with the default values, you do not include parentheses.
point p; // default constructor called
It is important to be aware of this syntax because it is easy to write the following by mistake:
point p(); // compiles, but is a function prototype!
This will compile because the compiler will think you are providing a function prototype as a forward declaration. However, you'll get an error when you attempt to use the symbol p as a variable. You can also call the default constructor using initialize list syntax with empty braces:
point p {}; // calls default constructor
Although it does not matter in this case, where the data members are built-in types, initializing data members in the body of the constructor like this involves a call to the assignment operator of the member type. A more efficient way is to use direct initialization with a member list.
The following is a constructor that takes two parameters, which illustrates a member list:
point(double x, double y) : x(x), y(y) {}
The identifiers outside the parentheses are the names of class members, and the items inside the parentheses are expressions used to initialize that member (in this case, a constructor parameter). This example uses x and y for the parameter names. You don't have to do this; this is only given here as an illustration that the compiler will distinguish between the parameters and data members. You can also use braced initializer syntax in the member list of a constructor:
point(double x, double y) : x{x}, y{y} {}
You call this constructor when you create an object like this:
point p(10.0, 10.0);
You can also create an array of objects:
point arr[4];
This creates four point objects, which can be accessed by indexing the arr array. Note that when you create an array of objects the default constructor is called on the items; there is no way to call any other constructor, and so you have to initialize each one separately.
You can also provide default values for constructor parameters. In the following code, the car class has values for the four tires (the first two are the front tires) and for the spare tire. There is one constructor that has mandatory values that will be used for the front and back tires, and an optional value for the spare. If a value is not provided for the spare tire pressure, then a default value will be used:
class car
{
array<double, 4> tire_pressures;;
double spare;
public:
car(double front, double back, double s = 25.0)
: tire_pressures{front, front, back, back}, spare{s} {}
};
This constructor can be called with either two values or three values:
car commuter_car(25, 27);
car sports_car(26, 28, 28);