Get the App
SLTechnology News&Howtos  ›  Database  › 

C++ overload assignment operator and 11 choose 5 platform repair

Shulou Source: shulou.com Published: 2022-06-01 12:16:21 10月03日 Update

1. What should be returned by the overloaded assignment function in C++?

11 choose 5 platform repair (enterprise E: 1793 408) class overload assignment operators generally exist as member functions, so what type should the function return? Refer to the assignment operation of the built-in type, such as

Int x,y,z

X=y=z=15

The assignment behavior is equivalent to x = (y = (zip15)), that is, the assignment operation should return a reference to the left Operand. Therefore, in order to be compatible with the built-in type, the overloaded assignment operator in the class should return a reference to the left Operand, namely * this, as shown in the declaration of the overloaded assignment operation function of class A

Class A {}

A & A / A / A

2. Ensure that the overload assignment operation has good behavior

For assignment operations, the first thing you should think about is how to handle self-assignment, especially when the class contains pointer-type data, as follows

Class MyString {

Public:

...

MyString (char * p=NULL)

MyString& operator= (const MyString&)

Private:

Char * str

}

MyString a ("hello")

MyString b ("world")

Abaca

We know that when assigning a value, we first release the resources of the left Operand, and then assign the left Operand according to the right Operand. The assignment function is as follows

Tags: Functions operands types operators behaviors platforms Cellular operators good important that is particularly members pointers data resources references processing operators operations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno Docker macOS Xiaomi