- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 68字
- 2021-06-10 18:28:13
Delegating constructors
A constructor may call another constructor using the same member list syntax:
class car
{
// data members
public:
car(double front, double back, double s = 25.0)
: tire_pressures{front, front, back, back}, spare{s} {}
car(double all) : car(all, all) {}
};
Here, the constructor that takes one value delegates to the constructor that takes three parameters (in this case using the default value for the spare).