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

How to use the C++ assignment operator

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the C++ assignment operator", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use the C++ assignment operator.

Class object assignment is allowed in C++, which is achieved through the default overloaded assignment operator, which is based on the following prototype:

Class_name & Class_name::operator= (const Class_name &)

It accepts and returns a reference to the class object.

When an existing object is assigned to another object, the overloaded assignment operator is used:

StringBad headline1 ("Celery"); StringBad knot;knot = headline1; / / calls the assignment operator

If it is the process of object initialization, the assignment operator is not necessarily used, such as:

StringBad metoo = knot

It's hard to say in a case like this, because metoo is a newly created object that can use a copy constructor. However, it can also be done in two steps, first using the copy constructor to create a temporary object, and then using the assignment operator to copy to the new object when assigning.

Similar to the copy constructor, the default assignment operator is implemented to copy the members one by one. If the member itself is a tired object, it will be copied using the assignment operator of this class.

What is the problem with the assignment operator? Let's take a look at the previous example of StringBad

Let's take a look at the following code:

StringBad sb ("test"); StringBad sports ("Spinach Leaves Bowl for Dollars"); StringBad knot;knot = sports

When we run it, we will encounter an error like this:

The reason for the error is clearly written in the log that we are trying to free up an unallocated memory.

The reason for the error is simple, because when we execute knot = sports, the string inside the two objects points to the same address. As a result, the corresponding content of the sports object no longer exists when the knot is destructed.

The solution is also simple: we overload the assignment operator ourselves to ensure that there is no simple copy problem.

StringBad & StringBad::operator= (const StringBad & st) {if (this = = & st) return * this; delete [] str; len = st.len; str = new char [len+1]; std::strcpy (str, st.str); return * this;} so far, I believe you have a better understanding of "how to use the C++ assignment operator". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report