Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of the list initialization mechanism in Category 11?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what is the concept of list initialization mechanism in Category 11". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Overview

Definition: list initialization is a new standard introduced by Category 11, which aims to unify the initialization method.

Previously, list initialization could only be used to initialize built-in type arrays and POD type objects. List initialization can be used to initialize any type of object.

POD (plain old data) type: a class that consists only of built-in type variables and does not contain pointers. Simply put, it is an object that can be copied directly using memcpy

Aggregate: the polymer must be of type POD

No custom constructor

Selfless or protected non-static data members (static members are independent of individual objects and therefore do not affect initialization)

No base class

No imaginary function

Non-static data members that have been initialized in no class

Note: distinguish between list initialization and initialization list

List initialization: initialization with {}

Initialization list: a list of object members initialized directly before the constructor body

Initializer_list: a lightweight STL container for undetermined parameters

Detailed explanation of implementation mechanism

The implementation details of list initialization for built-in type objects, POD objects, and class objects are different

List initialization of POD type

Here POD types include: built-in types, aggregate classes

The array of built-in types is initialized sequentially

List initialization in the Category 11 standard prevents the reduction of types that may lead to potential loss of information (that is, large types such as int cannot be implicitly converted to small types such as char) like assignments.

Aggregate classes are initialized in the order in which members are defined

List initialization of classes with constructors (Category 11)

The initialization through {} is consistent with the result of () (that is, {} can be replaced by {} where the constructor is called by ()), and the object is directly initialized by calling the corresponding constructor with the values in parentheses. It will not be copied as a temporary object.

= {} and {} are equivalent syntax [that is, whether adding = has no effect on initialization behavior], neither the copy operator nor the copy constructor is called.

Consistent with the list initialization of built-in types, the list initialization of Clippers 11 can only be used for initialization, not for the assignment of initialized objects

The actual mechanism guesses that the actual parameter passed is of type initializer_list, and the call is realized by matching the overloaded function. [I don't know how to verify this process, get the boss's solution]

List initialization is used for function return values

Initialization of a list of {} can be returned in a function whose return type is an object (not a reference to an object)

{} the actual type of the return value is initiallizer list (but cannot be declared as std::initializer_list), which is equivalent to the expression that returns the constructor, so the type cannot be a reference to the object.

Introduction of std::initializer_list

Initializer_list is a lightweight STL template, declared in the header file and defined in the namespace std

Any STL container has the same initialization ability as an array of unspecified length, and can be filled with any amount of data of the same type, so you can easily assign values to classes of a fixed type with the STL container.

Initializer_list is a lightweight template that can accept any length of data of the same type, that is, variable length parameters. As a STL container, it has the common characteristics of STL containers (such as iterators).

There are only three member interfaces: begin () end () size ()

It can only be initialized and assigned as a whole, and the data traversed by the iterator is only readable and cannot be modified on a single data.

All {} objects are literals of std::initializer_list type implicitly created (right value) and are widely used for list initialization (no header files are required)

Code verification

Class testClass {private: int a; int b * * public: testClass (): a (0), b (0) {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report