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 are the differences between C++ direct initialization and replication initialization?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what is the difference between C++ direct initialization and copy initialization". In daily operation, I believe many people have doubts about the difference between C++ direct initialization and copy initialization. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts of "what is the difference between C++ direct initialization and copy initialization"! Next, please follow the small series to learn together!

1. The statement in Primer

Let's first look at what Scripture says:

"When used with class-type objects, the copied form of initialization differs from the direct form: direct initialization directly calls the constructor matching the argument, copy initialization always calls the copied constructor. copy initialization first creates a temporary object using the specified constructor, and then copies that temporary object to the object being created using the copy constructor "

There is another passage that says:

In general, direct initialization and copy initialization differ only in low-level optimizations, however, for types that do not support copy, or when non-explicit constructors are used, they differ substantially:

1

2

ifstream file1( "filename" ): //ok:direct initialization

ifstream file2 = "filename" ; //error:copy constructor is private"

Common misconceptions

From the above statement, we can know that direct initialization does not necessarily call the copy constructor, and copy initialization must call the copy constructor. However, most people think that direct initialization is to call the copy constructor when constructing an object, while copy initialization is to call the assignment operator (operator=) when constructing an object. This is a big misunderstanding. Because initialization occurs only when an object is created, assignment does not apply to object creation, and there is no such thing as a primer. As for why this misunderstanding occurs, it may be because there is an equal sign (=) in the writing of copy initialization.

To clarify the problem, or to explain it from the code is easier to understand, please look at the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

#include

#include

using namespace std;

class ClassTest

{

public :

ClassTest()

{

c[0] = '\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

Internet Technology

Wechat

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

12
Report