我正在编写这个复制构造函数:
//CCtor of RegMatrix
RegMatrix::RegMatrix(const RegMatrix &other){
this-> numRow = other.getRow();
this-> numCol = other.getCol();
//Create
_matrix = createMatrix(other.numRow,other.numCol);
int i,j;
//Copy Matrix
for(i=0;i<numRow; ++i){
for(j=0;j<numCol; ++j){
_matrix[i][j] = other._matrix[i][j];
}
}
}
像这样初始化列表中的numRow、numCol是不是有问题:
numRow(other.numRow), numCol(other.numCol)
而不是:
this-> numRow = other.getRow();
this-> numCol = other.getCol();
还有,不知道有没有这样的问题,是不是在初始化列表中调用其他类的对象的函数有问题,比如:
numRow(other.getRow())
而不是:
this-> numRow = other.getRow();
初始化列表中的numRow、numCol初始化是否有问题[...]?
一般来说,这样做有两个问题:
在您的具体示例中这并不重要,因此您可以安全执行此操作。
不,实际上没有问题。但要注意初始化的顺序不是您在 init-list 中指定初始值设定项的顺序。该顺序是您的成员在班级中声明的顺序。除了那个潜在的问题之外,我没有看到任何问题。 HTH