C++11之前的状况
构造函数多了以后,几乎必然地会出现代码重复的情况,为了避免这种情况,往往需要另外编写一个初始化函数。例如下面的Rect类:
struct Point{int x;int y;
};
struct Rect{
Rect(){
init(0, 0, 0, 0, 0, 0);
}
Rect(int l, int t, int r, int b){
init(l, t, r, b, lc, fc, 0, 0);
}
Rect(int l, int t, int r, int b,int lc, int fc){
init(l, t, r, b, lc, fc);
}
Rect(Point topleft, Point bottomright){
init(topleft.x, topleft.y,bottomright.x, bottomright.y,0, 0);
}
init(int l, int t, int r, int b,int lc, int fc){
left = l; top = t;right = r; bottom = b;
line_color = lc;
fill_color = fc;//do something else...
}
int left;
int top;
int right;
int bottom;
int line_color;
int fill_color;
};
数据成员初始化之后要进行某些其他的工作,而这些工作又是每种构造方式都必须的,所以另外准备了一个init函数供各个构造函数调用。
这种方式确实避免了代码重复,但是有两个问题:
没有办法不重复地使用成员初始化列表
必须另外编写一个初始化函数。
C++11的解决方案
C++11扩展了构造函数的功能,增加了委托构造函数的概念,使得一个构造函数可以委托其他构造函数完成工作。使用委托构造函数以后,前面的代码变成下面这样:
struct Point{int x;int y;
};
struct Rect{
Rect():Rect(0, 0, 0, 0, 0, 0){}
Rect(int l, int t, int r, int b):Rect(l, t, r, b, 0, 0){}
Rect(Point topleft, Point bottomright)
:Rect(topleft.x, topleft.y,bottomright.x, bottomright.y,0, 0){}
Rect(int l, int t, int r, int b,int lc, int fc)
:left(l), top(t), right(r),bottom(b),line_color(lc), fill_color(fc){
//do something else...
}
int left;
int top;
int right;
int bottom;
int line_color;
int fill_color;
};
真正的构造工作由最后一个构造函数完成,而其他的构造函数都是委托最后一个构造函数完成各自的构造工作。这样即去掉了重复代码又避免了前一种方法带来的问题。
通过代码可以看出:委托构造函数的语法和构造函数中调用基类构造函数一样。调用顺序,效果什么也差不多。
作者观点
DRY(Don't repeat yourself )也需要开发环境的支持。
觉得本文有帮助?请分享给更多人。
扫码关注微信公众号【面向对象思考】,轻松学习每一天!
面向对象设计,面向对象编程,面向对象思考!
网友评论