In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.