Definition

xiaoxiao2021-03-06  63

Love the Thinking in series, so I played this name. The idea of ​​this article also comes to this book, or refers to comparison, or in-depth mining, or makes picking out, or has a feeling, including Thinking In C , and even including Thinking in Java.

Thinking Again In C (4) Definition

Keywords: C , operator, Operator, overload, overload, form derived

Big premise

Theorem: Customized types do not have no valuable, incompatible with built-in types (Effective C 2e). This theorem is the theoretical basis of all content of this article, which shows its importance. But I can't prove that it just thinks it is correct, if allowed, I am more happy to call it axiom.

Small premise

Rules of built-in types: int A = 1, b = 2, c = 3; A = (b = c); // [a] OK (B = c) = a; // [b] OK (A B) = C; // [C] Error (a = b) = C; // [D] OK ( a) = B; // [E] OK (A ) = B; / / [F] Error

in conclusion

Member function version operator overloaded declaration form: class arrow {private: double x; double y; public: arrow & operator = (const arrow & r); // [1] const arrow operator (const arrow & r) const; / / [2] Double Operator * (const arrow & r) const; // [3] arrow & operator = (const arrow & r); // [4] arrow & operator (); // [5] const arrow operator (int); // [6]}; Arrow's true meaning is vector, that is, there is a size and direction in mathematics. The original English name should be a vector, but there is already a similar name but a meaningful thing in STL. Take this non-class name in order to distinguish. For this class, operators are meaningless, in addition, assignment operators use the default version of the compiler, and write them purely.

Deducement

The behavior of the overload operator is consistent with the built-in type by reasonably using the const modifier and reference.

Single resolution

[1] Due to the relationship between [A] [B], the return value of the = operator should be rewritten, so it is transmitted back * this reference. Arrow & Arrow :: Operator = (const arrow & r) {x = r.x; y = r.y; return * this;} [2] operator does not change the left operating number, so the function is constant. The result is stored in the Local object of the function. For security considerations, the local object cannot be returned as a reference, and because the relationship between [C], the Const copy of the local object is passed. Const arrow arrow :: Operator (const arrow & r) const {return arrow (x r.x, y r.y);} Arrow constructor omitted.

[3] The return value of the built-in type cannot be left value by the compiler, and the return value can be omitted. Other same [2]. Double arrow :: operator * (const arrow & r.x y * r.y;} vector multiplied by each of them, the volume is the number instead of vector.

[4] Due to the relationship between [D], the same [1]. Arrow & Arrow :: Operator = (const arrow & r) {x = r.x; y = r.y; return * this;}

[5] The prefix version of the self-incremental operator returns the changed object, but also due to the relationship between [E], the reference to * this. Arrow & Arrow :: Operator () {// ... return * this;}

[6] The suffix version of the self-incremental operator returns the object before changing, so a Local object must be established to save the original state, so the function returns value [2]. Const arrow arrow :: Operator (int) {arrow arr (* this); // ... return arr;}

Reverse push

It can be seen from [1] [4] [5], we can naturally provide [A] [B] [D] [E], while ensuring sufficient rigor. For [2], there is no security means, the form of [c] is compiled. Also, why is the built-chain rules [e] [f] of the built-in type, it can be seen from [5] [6], because the function can safely return the changed object, but cannot be safely returned. Change the object before. These have increased the credibility of the big prerequisites, and small premises are also more reasonable.

Postscript: 1. Limited to the space, here only list the simplest operator overload, omit a lot of discussions about complex, special operators. 2. * This type of binary operator is best adopted in the form of a non-member form. If you want to see a further discussion, I will write a little more.

转载请注明原文地址:https://www.9cbs.com/read-121470.html

New Post(0)