1

I'm reading Stroustrup C++ 4th Ed. Page 162 Types and Declarations. Specifically, where the following is allowed to construct a complex<> object.

The book comments that case A is "use constructor". Is case A really a list initializer and the constructor method of initialization is performed in B or C?

#include <iostream>
#include <complex>
using namespace std;

int main(int argc, char *argv[])
{
    complex<double> z = { 0, 3.14 }; // A
    complex<double> h(0, 3.14); // B
    complex<double> i{0, 3.14}; // C

    return 0;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
notaorb
  • 1,944
  • 1
  • 8
  • 18
  • All three cases are initialised using the same constructor. B does direct initialisation. Since C++11, cases A and C do aggregate initialisation (or, more specifically, copy list initialisation and direct list initialisation, respectively). – Peter Jun 03 '20 at 04:13
  • Is one better than the other? All these ways of initializing an object is becoming confusing :( – notaorb Jun 03 '20 at 20:25
  • They are alternative ways of performing initialisation. – Peter Jun 04 '20 at 09:06
  • @notaorb There're subtle differences among them, even not in this case. See [Why is list initialization (using curly braces) better than the alternatives?](https://stackoverflow.com/q/18222926/3309790) – songyuanyao Jun 04 '20 at 09:38

1 Answers1

0

The effects are all the same for this case; objects are initialized by the constructor complex::complex(double, double).

songyuanyao
  • 169,198
  • 16
  • 310
  • 405