Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the differences between C++ direct initialization and replication initialization?

Shulou Source: shulou.com Published: 2022-06-01 20:49:59 09月20日 Update

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

Tags: Functions objects compilers statements results differences compilers code parameters facets run or private clocked reason that is learning different essence comments Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple macOS MariaDB Shulou Technology Shulou Tech Info