In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the copy constructor in C++, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
Detailed explanation of C++ copy constructor (copy constructor)
Copy and copy have the same meaning, and the corresponding English word is copy. For computers, copying means creating a new piece of data from an existing piece of data, and the end result is an additional copy of the same data. For example, copying Word documents to a U drive to a photocopy store to print, copying pictures from disk D to the desktop for easy browsing, uploading important files to Baidu disk to prevent loss, etc., all mean "create a new data".
In C++, copy does not deviate from its original meaning, but simply "specializes" the meaning, which means to create a new object from an existing object. In essence, an object is also a piece of data because it takes up memory.
Strictly speaking, the creation of an object consists of two phases, first allocating memory space, and then initializing:
Allocating memory is easy to understand, which is to leave enough bytes in the stack area, stack area, or global data area. At this time, the memory is still relatively "primitive" and has not been "civilized". The data it contains is generally zero or random, which has no practical significance.
Initialization is the first time you assign a value to memory to make its data meaningful. Note that this is the first assignment, and another assignment is not initialization. During initialization, you can also allocate other resources to the object (open files, connect to the network, dynamically allocate memory, etc.). Or do some calculations in advance (calculate the total price according to the price and quantity, calculate the area of the rectangle according to the length and width, etc.). To put it bluntly, initialization is a call to the constructor.
Obviously, the copy here is made during the initialization phase, that is, the memory of the new object is initialized with the data of other objects.
So, how do you initialize an object by copying it? In fact, such examples abound, and the string class is a typical example.
# include # include using namespace std;void func (string str) {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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.