Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can we use list initialization?

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.

like image 450
Luchian Grigore Avatar asked Nov 07 '12 09:11

Luchian Grigore


People also ask

What are the advantages of initializer list?

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.

Does Java have initialization list?

Using Collections.addAll()Collections class has a static method addAll() which can be used to initialize a list.

Does initializer list call constructor?

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.

What is std :: initializer list?

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 .


1 Answers

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.

like image 99
David G Avatar answered Oct 22 '22 00:10

David G