This question already covers what PODs and aggregates are, and provides some examples on aggregate initialization.
The question here is where can you use list initialization?
Also where can you use (in lack of a better term) list assignment?
An answer should deal with both C++03 and C++11, highlighting the differences between them.
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.
Using Collections.addAll()Collections class has a static method addAll() which can be used to initialize a list.
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.
An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .
List-initialization can be used to initialize dynamically-allocated arrays (C++11):
int * a = new int[3] {4, 3, 2};
A very nifty feature not possible in C++03.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With