Member initializer list notation: curly braces vs parentheses

Viewed 6316

Consider the following code snippet from pg. 17 's A Tour of C++:

class Vector {
public:
  Vector(int s) :elem{new double[s]}, sz{s} { } //construct a Vector
  double& operator[](int i) { return elem[i]; } //element access: subscripting
  int size() { return sz; }
private:
  double* elem; // pointer to the elements
  int sz;  // the number of elements
};

Here I'm concerned about the member initializer list on the third line, where Stroustrup separates a colon from the two initializer statements elem{new double[s]} and sz{s}.

Question: Why here does he use curly braces (i.e., {..}) to make these two initializer statements? I have seen elsewhere on the web people making initializer lists with parentheses, such that this could also (AFAIK) legally read elem(new double[s]) and sz(s). So is there a semantic difference between these two notations? Are there other ways one could initialize these variables (within the context of an initializer list)?

1 Answers
Related