Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of C++ operator overloading

Shulou Source: shulou.com Published: 2022-06-03 15:36:03 10月03日 Update

This article mainly introduces the example analysis of C++ operator overloading, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

1. Operator overloading

The C++ operator can only be used with basic data types

Expression form

Return value type operator operator (formal parameter table) {.} 1.1 operator is overloaded as a normal function

1.2 operators are overloaded as member functions

two。 Overloading of the assignment operator =

When the types on both sides of the assignment operator do not match, for example, the int type is assigned to the Complex type, in this case, the assignment operator = needs to be overloaded, and the assignment operator = can only be overloaded as a * * member function * *, not an ordinary function.

Pointing dynamic

2.1 shallow replication and deep replication

Shallow copy: copy byte by byte

Deep copy: copies what the pointer variable points to to what the pointer member object points to in another object

In order to achieve the above effect, you need to design a new operator=, implementation program as follows:

2.2 discussion of return values

3. Dynamic variable length array

Thank you for reading this article carefully. I hope the article "sample Analysis of C++ operator overloading" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Operators operations types functions articles examples analysis normal content dynamic objects pointers values interests variables and at the same time bytes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple OPPO Reno NVidia macOS MariaDB