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

The specific use of how to initialize the list in the new features of Category 11

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the specific use of how to initialize the list in the new features of Clover 11. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

In our actual programming, we often encounter the problem of variable initialization. There are a variety of initialization methods for different variables. For example, we can initialize an array using int arr [] = {1 ~ 2 ~ 3}, or for a simple structure:

Struct A {int x; int y;} a = {1pm 2}

These different initialization methods have their own scope of application and functions, and this initialization method can not be used for classes, the most important thing is that there is no universal initialization method suitable for all scenarios. Therefore, in order to unify the initialization mode, the concept of list initialization (list-initialization) is put forward.

Unified initialization method

The list initialization can only be used for ordinary arrays and POD (plain old data, which simply means objects that can be copied with memcpy) in Cards 98Compact 03, as follows:

The initialization list of the array: int arr [3] = {1pm 2pm 3}

Initialization list of POD type:

Struct A {int x; int y;} a = {1pm 2}

The initialization list is enlarged in Category 11 and can be used for initialization of any type of object. As follows:

Class Foo {public: Foo (int) {} private: Foo (const Foo &);}; int _ tmain (int argc, _ TCHAR* argv []) {Foo A1 (123); / / call Foo (int) constructor to initialize Foo a2 = 123 The copy constructor of / / error Foo is declared private, initialized by implicitly calling the Foo (int) constructor to generate a temporary anonymous object, and then calling the copy constructor to initialize Foo a3 = {123}; / / list initialization Foo a4 {123}; / / list initialization int a5 = {3}; int a6 {3}; return 0;}

Can be seen from the above sample code, in Category 11, list initialization can not only complete the initialization of the common type, but also complete the initialization of the list of classes, it should be noted that A3 A4 is a list initialization, private copy does not affect it, only call the constructor of the class and do not need to copy constructor, the writing of a4jin9803 does not have, it is a new way of writing Category 9803.

At the same time, the list initialization method is also suitable for initialization with parentheses such as new operation, as follows:

Int* a = new int {3}; double b = double {12.12}; int* arr = new int [] {1,2,3}

What's amazing is that you can use the list initialization method to initialize an array of memory allocated in the heap in Clover 11, which is not possible in Clover 98. 03.

Some usage details of list initialization

Although list initialization provides a unified initialization method, it also brings some confusion in use that programmers need to pay attention to, such as the following examples of custom types:

Struct A {int x; int y;} a = {123,321}; / a.x = 123a.y = 321 struct B {int x; int y; B (int, int): X (0), y (0) {}} b = {123321}; / / b.x = 0b.y = 0

For custom struct A, using list initialization does not cause problems for ordinary POD types. XQuery y is initialized correctly, but the difference between structure B and structure An is that structure B defines a constructor and uses a member initialization list to initialize B's two variables, so list initialization doesn't work here. B uses the constructor to complete the initialization of the variable.

So how can you tell whether a class (class struct union) can use list initialization to complete the initialization work? The key question is whether the class is an aggregate. First, take a look at the C++ definition of whether the class is an aggregate:

(1) No user-defined constructor.

(2) selfless or protected non-static data members

(3) No base class

(4) No imaginary function.

(5) there are no {} and = directly initialized non-static data members. Let's analyze the above one by one.

1. First of all, there is a user-defined constructor, as shown below:

Struct Foo {int x; int y; Foo (int, int) {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