mercredi 29 juillet 2015

C++11 constructor initializer with different syntaxes [duplicate]

This question already has an answer here:

I ended up on this page in the references.

I got somewhat confused with this example:

class X {
    int a, b, i, j;
public:
    const int& r;
    X(int i)
      : r(a) // initializes X::r to refer to X::a
      , b{i} // initializes X::b to the value of the parameter i
      , i(i) // initializes X::i to the value of the parameter i
      , j(this->i) // initializes X::j to the value of X::i
    { }
};

Is there any difference between using the bracketed initializer list syntax like b{x} and the traditional parenthesis syntax b(x)?

When should I use each one of them?

Aucun commentaire:

Enregistrer un commentaire