Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the use of C++ definition operators

Shulou Source: shulou.com Published: 2022-06-02 05:17:27 10月14日 Update

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

Operators are defined mainly to imitate idioms

Reason (reason)

Minimize surprises.

Make the program look more natural.

Example (sample)

Class X {

Public:

/ /...

X & operator= (const X &); / / member function defining assignment

Friend bool operator== (const const, const X &); / / = = needs access to representation

/ / after a = b we have a = = b

/ /...

}

Here, the conventional semantics is maintained: Copies compare equal.

Here, the customary semantics of assignment and equality comparison are maintained.

Example, bad (negative example)

X operator+ (X a, X b) {return a.v-b.v;} / / bad: makes + subtractNote (note)

Nonmember operators should be either friends or defined in the same namespace as their operands. Binary operators should treat their operands equivalently.

The non-member operator should be either a friend function or defined with an Operand. Binary operators should treat operands equally.

At this point, I believe you have a better understanding of "what is the use of the C++ definition 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: Operators operations examples content objects examples either learning practical deeper binary interest function reason negative practical practical member simple operation method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker vpn Shulou Information Microsoft