This first problem highlights the importance of understanding what you write. Here we have a few simple lines of code — most of which mean something different from all the others, even though the syntax varies only slightly.



Problem



JG Question



1. What is the difference, if any, among the following?

widget w; // (a)



widget w(); // (b)

widget w{}; // (c)



widget w(x); // (d)

widget w{x}; // (e)



widget w = x; // (f)

widget w = {x}; // (g)



auto w = x; // (h)

auto w = widget{x}; // (i)

Guru Questions



2. What do each of the following lines do?

vector<int> v1( 10, 20 ); // (a)



vector<int> v2{ 10, 20 }; // (b)

3. Besides the cases above, what other benefits are there to using { } to initialize objects?

4. When should you use ( ) vs. { } syntax to initialize objects? Why?