Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any problem of calling functions in the initialization list?

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();
like image 635
limlim Avatar asked Oct 10 '10 08:10

limlim


People also ask

What is the purpose of initializer list?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

Does initializer list call constructor?

An initialization list can be used to explicitly call a constructor that takes arguments for a data member that is an object of another class (see the employee constructor example above). In a derived class constructor, an initialization list can be used to explicitly call a base class constructor that takes arguments.

What are the advantages of initializer list?

The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.

Is initialization list faster?

Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment. Note: There is no performance difference if the type of x_ is some built-in/intrinsic type, such as int or char* or float .


1 Answers

Is there a problem to initialize numRow, numCol in the initialization list [...]?

In general, there's two problems with doing so:

  1. While initializing objects in the initialization list, the object is not yet fully constructed. Therefore, when you're invoking non-static member functions, you are invoking them on a not yet fully constructed object. If those functions attempt to use any sub-object of the object that has not been constructed, you are invoking Undefined Behavior.
  2. The order of initialization is the order of declaration of the members in the class definition, it is not the order in which they are listed in the initialization list. Therefore you need to pay attention to initialization of members requiring data from other members. (This can be seen as a sub-problem of the previous: using not yet constructed sub-objects.) It is best to avoid such situations, but if they cannot be avoided, add a big, scary comment to where the members are declared in the class' definition, emphasizing the importance of their order.

In your concrete example this doesn't matter, so you are safe to do this.

like image 88
sbi Avatar answered Sep 20 '22 10:09

sbi