lun*_*una 6 c++ syntax initialization ctor-initializer
对于下面的C++函数:
cross(vector<int> &L_, vector<bool> &backref_, vector< vector<int> > &res_) :
L(L_), c(L.size(), 0), res(res_), backref(backref_) {
run(0);
}
Run Code Online (Sandbox Code Playgroud)
冒号(":")告诉左右两部分之间的关系是什么?可能,这段代码可以说什么呢?
这是一种在实际调用类的c'tor之前初始化类成员字段的方法.
假设你有:
class A {
private:
B b;
public:
A() {
//Using b here means that B has to have default c'tor
//and default c'tor of B being called
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在通过写作:
class A {
private:
B b;
public:
A( B _b): b(_b) {
// Now copy c'tor of B is called, hence you initialize you
// private field by copy of parameter _b
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |