记录一:
使用初始化列表来初始化字段:
Line::Line( double len): length(len)
{
cout << "Object is being created, length = " << len << endl;
}
上面的语法等同于如下语法(常用):
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
进一步:
C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
....
}
例子:
注意:只有构造函数才可以这样用。
网友评论