Site icon Learn C++

What Are The Member Initializers And Aggregates Features in C++14?

What Are The Member Initializers And Aggregates Features in C++14

C++14 brought us a lot of useful things that we use today. One of the great features of C++14 was the member initializers and aggregates feature that a class with default member initializers may now be an aggregate. In this post, we explain how can use member initializers and aggregates with examples.

What are the member initializers and aggregates features in C++14?

With the new C++14, a class or struct with default member initializers may now be an aggregate in definition.

If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list“. Here is an example,

[crayon-6711d92e6c13a255279224/]

Here, X.b will be 100 automatically.

Are there simple examples about member initializers and aggregates features in C++14?

This feature can be good to be used with default values (i.e. “Unknown”), here is an example.

[crayon-6711d92e6c142190687164/]

Here, David will have 0 score and “Unknown” university, Yilmaz will have “Unknown” university.

We can use parameters in the next parameters.

[crayon-6711d92e6c144691870929/]

Here example above, value of test.val is initiated automatically to 67 which is third char ‘C’.

Is there a full example about member initializers and aggregates feature in C++14?

Here is a full example.

[crayon-6711d92e6c145102885339/]

The output of this example will be as follows:

[crayon-6711d92e6c146669365330/]

For more details, please see,
this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3605.html ,
and this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3653.html

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version