Home > Backend Development > C++ > body text

How Does C 11 Allow In-Class Initialization of Non-Static and Non-Constant Members?

Patricia Arquette
Release: 2024-11-20 02:05:02
Original
762 people have browsed it

How Does C  11 Allow In-Class Initialization of Non-Static and Non-Constant Members?

C 11's In-Class Initialization: Relaxing Restrictions for Non-Static and Non-Constant Members

Before C 11, in-class initialization was restricted to static const integral or enumeration type members. The reasoning behind this limitation was the need to maintain unique definitions for objects across multiple translation units while avoiding complicated linker rules.

However, C 11 relaxes these restrictions, allowing in-class initialization of non-static members (§12.6.2/8). This includes non-static data members with brace-or-equal-initializers and non-virtual base classes of non-abstract classes.

So, what has changed to make this possible? While no significant modifications were made to the linker, the compiler has become more complex to handle the additional responsibilities.

Unlike previous versions of C , which enforced strict rules on multiple definitions, C 11's compiler now handles multiple definitions within a class. Instead of the linker resolving definition conflicts, the compiler takes on this task.

This change in responsibility introduces more complexity for programmers who must now consider potential conflicts between multiple member initializations for a single member. For example, a class with both a member initializer and a constructor with a specific initialization for the same member requires careful consideration to determine which initialization will be used.

Despite the increased complexity, the benefits of in-class initialization outweigh the challenges. It allows for more concise and expressive code, reducing boilerplate and making code easier to read and understand.

The above is the detailed content of How Does C 11 Allow In-Class Initialization of Non-Static and Non-Constant Members?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template