Initialization of Objects in C++
Initialization in C++
C++11 and C++20 introduce several new initialization techniques for variables and classes. These techniques help improve code readability, maintainability, and performance. In this article, we will take a closer look at each of these techniques and see examples of how they can be used in code with a class called Entity.
1. Uniform Initialization
This is a new initialization technique introduced in C++11. It allows for initializing variables and objects using curly braces { }. This technique is also known as brace initialization. For example, int x{5}; or std::string name{"John"};
2. Initializer Lists
C++11 also introduced the concept of initializer lists, which allows for initializing objects of container classes, such as std::vector, std::array, and std::initializer_list. For example, std::vector<int> v{1, 2, 3};
3. Constructor Delegation
C++11 introduced a new feature called constructor delegation, which allows a constructor to call another constructor of the same class. This helps in reducing code duplication and improves code maintainability. For example, class A { A( ); A(int x); }; A::A(): A(0) { };
4. Default Member Initialization
C++11 introduced the ability to initialize class members with a default value. This eliminates the need to initialize members in the constructor's initializer list. For example, class A { int x = 0; };
5. Aggregate Initialization
C++11 introduced the concept of aggregate initialization, which allows for initializing structs and classes that have no user-defined constructors, virtual functions, private members or base classes. In other words, an aggregate class is a class with no user-provided constructors, no private or protected non-static data members, no base classes and no virtual functions. For example, struct A { int x; int y; }; A a{1, 2};
This is an example of aggregate initialization, where the class Entities is considered as an aggregate class because it has no user-provided constructors, no private or protected non-static data members, no base classes and no virtual functions. The values 1 and 2 are used to initialize the x_ and y_ members respectively.
It's worth noting that aggregate initialization can only be used when the members of the class are public. If any member is private or protected, the class cannot be an aggregate class and thus cannot be initialized using aggregate initialization. Also, in C++20, aggregate initialization can be used even when the class has user-defined constructors as long as they are all trivial and no base classes or virtual functions.
6. Designated Initializers
C++20 introduced designated initializers, which allow for initializing structs and arrays in a more readable and maintainable way. For example, struct A { int x; int y; } a = {.y = 2, .x = 1};
In summary, C++11 and C++20 introduce several new initialization techniques that improve code readability, maintainability, and performance. Some of the most commonly used techniques include uniform initialization, initializer lists, constructor delegation, default member initialization, aggregate initialization, and designated initializers.








