Thinking again in C ++ (4) operator reload form Derivation

Source: Internet
Author: User

I love thinking in series, so I started this name. The idea of this article is also part of this set of books, for example, comparison, or in-depth mining, or to make up for wisdom, or to make sense, including thinking in C ++, it even includes thinking in Java.

Thinking again in C ++ (4) operator reload form Derivation

Keywords: C ++, operator, operator, overload, overload, Formal Derivation

Prerequisites

Theorem: custom types should not be incompatible with built-in types without value (valid C ++ 2e ). This theorem is the theoretical basis of all the content in this article. We can see its importance. But I cannot prove it. I just think it is true intuitively. If I allow it, I prefer to call it justice.

Prerequisites

Built-in operator string chain rules:
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

Conclusion

Declaration Form of Operator Overloading for member functions:
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]
};
The true meaning of Arrow is vector, that is, the amount of size and direction in mathematics. Originally, the English name should be vector, but STL already has the same name but different meanings, in order to show the difference, we get the name of this nondescribable class. For this class, the ++ operator is meaningless. In addition, the assignment operator can use the default version of the compiler. Writing them is purely an example.

Derivation means

By reasonably using the const modifier and reference, the behavior of the overload operator is consistent with that of the built-in type.

Single Parsing

[1] because of the relationship between [a] [B], the return value of the + = operator should be rewritten, so the reference of * This is returned.
Arrow & Arrow: Operator + = (const arrow & R)
{
X + = R. X;
Y + = R. Y;
Return * this;
}

The [2] + operator does not change the left operand, so the function is of the const nature. + The result is stored in the local object of the function. For security reasons, the local object cannot be returned as a reference, and because of the [c] relationship, it is returned to the const copy of the local object.
Const arrow: Operator + (const arrow & R) const
{
Return arrow (x + R. X, Y + R. Y );
}
The constructor of Arrow is omitted.

[3] The returned value of the built-in type cannot be left as agreed by the compiler. In this case, the const of the returned value can be omitted. Others are the same as [2].
Double arrow: Operator * (const arrow & R) const
{
Return x * R. x + y * R. Y;
}
Returns their inner product by multiplying the vectors. The inner product is a number instead of a vector.

[4] because of the relationship [d], it is the same as [1].
Arrow & Arrow: Operator = (const arrow & R)
{
X = R. X;
Y = R. Y;
Return * this;
}

[5] The prefix version of the auto-increment operator returns the changed object and returns the reference of * this because of the [e] relation.
Arrow & Arrow: Operator ++ ()
{
//...
Return * this;
}

[6] The suffix version of the auto-increment operator returns the object before the change. Therefore, a local object must be created to save the original state. Therefore, the return value of the function is the same as that of [2].
Const arrow: Operator ++ (INT)
{
Arrow Arr (* This );
//...
Return arr;
}

Reverse push

From the implementation of [1] [4] [5], we can naturally provide [a] [B] [d] [e], while ensuring sufficient rigor. For [2], there is no security means to compile the formula like [C. Also, why are there so many subtle differences between the built-in string chain rules [E] [f], as shown in [5] [6, because the function can safely return the changed object, it cannot return the object before the change.
These increase the credibility of the big premise theorem, and the small premise is more reasonable.

Postscript:
1. Limited space, only the simplest Operator Overloading is listed here, and many discussions about complex and Special operators are omitted.
2. +, * this type of binary operators should preferably adopt the form of non-member, that is, the form of friends.
If you want to see further discussions, I will write a bit more.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.