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 copy methods in C++

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about the copy methods in C++. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Shallow copy

The shallow copy is quite simple for the other two, and the code is as follows:

String::String (const String & s)

: _ str (s._str)

, _ size (s._size)

, _ capacity (s._capacity)

{}

String & String::operator= (const String & s)

{

If (_ str! = s._str)

{

Delete [] _ str

_ str = s._str

_ size = s._size

_ capacity = s._capacity

}

Return * this

}

Although this method is very simple, there is a big loophole in this method, in which the overloaded object of the copy construction and assignment operator points to the same space as the copied object. if you use one of the objects to modify the value in the other object, it will also cause the value in the other object to change, so shallow copy will not be used in general.

However, when the value in the object cannot be changed is a const constant, the object can only be read and cannot be modified. Using shallow copy can reduce the memory overhead and will not be a problem.

Deep copy

By understanding the shallow copy, we find that the problem with the shallow copy is that the value in the object cannot be changed due to the lack of new space. But the values in most of our objects need to be modified, so deep copies are created to solve this problem. Since the content in the object cannot be changed at will if the pointing space is the same, the deep copy first opens up space for the object to be copied, and then copies the content over. Deep copy can be divided into two methods: traditional writing and modern writing, and the code is as follows:

Class String

{

Public:

String (const char* str = "")

String (const String& s)

String& operator= (const String& s)

~ String ()

Private:

Char* _ str

}

/ / complete deep copy of String by traditional writing method

/ / String::String (const char* str)

/ /: _ str (new char [strlen (str) + 1])

/ / {

/ / strcpy (_ str, str)

/ /}

/ /

/ / String::String (const String& s) / / reference must be used or it will be infinitely recursive

/ /: _ str (new char [strlen (s._str) + 1]) / / Open up space the size of s._str. + 1 is because of'/ 0'.

/ / {

/ / strcpy (_ str, s._str)

/ /}

/ /

/ / String& String::operator= (const String& s)

/ / {

/ / if (this! = & s) / / judge whether it is the same or not

/ / {

/ / if (_ str)

/ / delete [] _ str; / / _ str is not empty, then you need to release space first.

/ / _ str = new char [strlen (s._str) + 1]

/ / strcpy (_ str, s._str)

/ /}

/ / return * this

/ /}

/ /

/ / String::~String ()

/ / {

/ / if (NULL! = _ str)

/ / {

/ / delete [] _ str

/ /}

/ /}

/ / complete deep copy of String by modern writing

String::String (const char* str)

: _ str (new char [strlen (str) + 1])

{

Strcpy (_ str, str)

}

String::String (const String& s)

{

String tmp (s._str); / / define a tmp object with s._str

Swap (_ str, tmp._str); / / swap pointers

}

String& String::operator= (String s) / / formal parameter call constructor

{

Swap (_ str, s._str)

Return * this

}

String::~String ()

{

If (NULL! = _ str)

{

Delete [] _ str

}

}

Compared with the two methods, modern writing is more recommended for the following reasons:

The amount of code in modern writing is relatively less and more efficient.

The copy construction is called in the assignment operator overloading of the modern writing method, and the constructor is called in the copy construction, so the modern writing method is more reusable and easier to manage.

In traditional writing, assignment operator overloading will first release the previously opened space, so that if the following re-opening space fails to copy, the original data will not be retained.

Although deep copy will once again open up space to increase memory footprint, it can solve the problem that shallow copy cannot do.

Copy while writing

In fact, many problems can be solved through the above two copy methods, but the shallow copy cannot be modified, and the deep copy takes up more space if it is not modified. Therefore, copy appears when writing. In fact, copying when writing combines the advantages of the above two methods and solves these problems by introducing a reference count.

Copying is more complicated than the above method, so let's explain here: copying requires a reference count when writing, so to add a location to store a reference count, we put this location four bytes in front of _ str, and we count the reference + + once every time we copy it. When we modify it, we first determine whether the reference count is 1, and if it is 1, modify it directly. Otherwise, re-open a piece of space, count the original references, and then modify it.

Let's simply write the code:

Class String

{

Public:

String (char* str = "")

: _ str (new char [(strlen (str) + 5)]) / / Open up 4 more bytes to store the reference count

{

* ((int*) _ str) = 1

_ str + = 4; / / the content in str after the reference count

Strcpy (_ str, str)

}

String (const String& s)

{

_ str = s. Copy; / / basically similar to a shallow copy

+ + GetRefCount ()

}

String& operator= (const String& s)

{

If (_ str! = s._str)

{

If (--(GetRefCount ()) = = 0)

{

Delete [] _ str

}

_ str = s._str

+ (GetRefCount ())

}

Return * this

}

~ String ()

{

If (--(GetRefCount ()) = = 0)

{

Delete [] (_ str-4)

}

}

Int& GetRefCount () / / returns the reference type, which can also be written as a pointer type

{

Return * ((int*) (_ str-4))

}

Private:

Char* _ str

}

These are the copy ways of C++ that the editor shared for you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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