views:

59

answers:

2

Hi, I'm writing this copy constructor:

//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];
        }
    }
}

Is there a problem to initialize numRow, numCol in the initialization list like this: numRow(other.numRow), numCol(other.numCol) instead of:

this-> numRow = other.getRow();
this-> numCol = other.getCol();

Also, i don't know if there isn't such a problem, is there a problem of calling other classes' object's function in the initialization list, such as:

numRow(other.getRow())

instead of:

this-> numRow = other.getRow();
A: 

No there is no problem actually. Except that be careful that the order of initialization is NOT the order in which you specify initializers in the init-list. The order is the one in which your members have been declared in the class. Other than that potential problem, I see none. HTH

Armen Tsirunyan
+2  A: 
sbi
In other words: there's no problem, as long as you're aware of the effects
valdo
@valdo: Thinking about this, it might be that invoking member functions on a not yet fully constructed object _technically_ causes _UB_ even if those member function do not access any of the not yet constructed members. I'm not sure about this. (In practice, of course, this is safe as long as you do not access any no yet constructed non-POD objects.)
sbi