Order of execution in initializer list in C++ Last Updated : 03 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite: Classes, Constructors, Initializer list In this article, we will discuss the order of execution in the initializer list in C++. Generally, the order of execution is from top to bottom and left to right. But a rare condition arises where this rule fails is when the initializer list is used in class. In the initializer list, the order of execution takes place according to the order of declaration of member variables. While using the initializer list for a class in C++, the order of declaration of member variables affects the output of the program. Program 1: C++ // C++ program to illustrate the // order of initializer in list #include <iostream> using namespace std; class MyClass { private: // Declared first int b; // Declared Second int a; public: MyClass(int value) : b(value), a(b * 2) { cout << b << " " << a; } }; // Driver Code int main() { // Create an object MyClass obj(10); return 0; } Output: 10 20 Program 2: C++ // C++ program to illustrate the // order of initializer in list #include <iostream> using namespace std; class MyClass { private: // Declared first int a; // Declared Second int b; public: MyClass(int value) : b(value), a(b * 2) { cout << b << " " << a; } }; int main() { // Create an object MyClass obj(10); return 0; } Output: 10 65528 Both the outputs are different because the execution takes place according to the order of declaration: In the first program, b is declared first, so b is assigned the value of 10 and then a is declared, later a is assigned b*2. So, the output is 10 20.In the second program, a is declared first and then b. So, first, a is assigned b*2, but b is not initialized yet. So, some garbage value is assigned to a. Later b is assigned the value of 10. Comment More infoAdvertise with us Next Article Designated Initializers in C _roronoa_ Follow Improve Article Tags : C/C++ Puzzles Placements C++ CPP-Basics Pointers C-Pointer Basics +2 More Practice Tags : CPPPointers Similar Reads std::initializer_list in C++ 11 The std::initializer_list class template was added in C++ 11 and contains many built-in functions to perform various operations with the initializer list. It provides member functions like a size(), begin(), end(), and constructor to construct, iterate, and access elements of the initializer list. T 6 min read When do we use Initializer List in C++? Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.ExampleC++#include < 7 min read Different Ways to Initialize an unordered_set in C++ An unordered_set is an associated container available in the C++ Standard Template Library(STL) that is used for unique elements without any specific ordering, it internally uses the working principle of a hashtable to store elements. Different ways to Initialize an unordered_set in C++ Initializati 6 min read Different Ways to Initialize a List in C++ STL Initializing a list means assigning some initial values to the list elements. In this article, we will learn different methods to initialize the list in C++. Letâs start from the easiest method:The easiest way to initialize a list is by passing the initial values inside an initializer list to its co 3 min read Designated Initializers in C Standard C90 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.In ISO C99 you can give the elements in random order, specifying the array indices or structure field names they apply to, and GNU C allo 4 min read Designated Initializers in C++ 20 With C++20, we get a convenient way of initializing data members. The new feature is called Designated Initializers and it might be familiar to C programmers. In other words, Designated Initializers are a new feature that has been introduced in C++20. It allows developers or programmers to initiate 5 min read Like