Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the C++ assignment operator

Shulou Source: shulou.com Published: 2022-06-02 04:29:13 10月03日 Update

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!

Tags: Operator operation object copy time clocked content function reason that is member point problem learning practical deeper clear two code example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft Shulou Tech Info OPPO Reno Huawei