C ++ heavy-duty operators

Source: Internet
Author: User

1. Pre-and Post-operators, left and right


In fact, we haven't figured out the difference between the left value and the right value for a long time. We only know that the Left value can be placed on the left side of the equal sign or on the right side of the equal sign, however, the right value can only be placed on the right side of the equal sign, which then forms an intuitive impression, knowing how to do it without making any mistakes. However, today I have a look at C ++, but I have found some new experiences.


This expression is obviously incorrect for expression a -- = 5. The reason is that the result is that the value of a is obtained after the auto-subtraction expression on the left of the equal sign is executed, then perform the auto-subtraction operation. Therefore, the final result is a right value, which is the value of a (before the change). Therefore, for the value assignment statement B = --, naturally, it won't be wrong.


For -- A = 5; such an expression is obviously different. First, perform the auto-subtraction operation, and then return the value of A (after the change). Naturally, the returned a value can be assigned again.
. That is to say, the front operator is the left value expression, while the back operator is the right value expression. So I thought of an idea a long time ago: ++ A ++ =
5; of course, experts must know at first glance that the error is correct. So I thought of several methods, that is, through the method of adding brackets.


1) ++ (A ++) = 5. In fact, the error message shows the cause: Error: Non-lvalue in increment.
Obviously, the expression in the auto-increment operator is not the left value, so it cannot be calculated by auto-increment. Oh, yes. A ++ is the right value, so it is wrong to perform the ++ operation again, because the ++ operation requires
Is the left value expression. The method here does not work.


2) (++ A) ++ = 5; this does not work either. The error message is: Error: Non-lvalue in.
Assignment, you can see through the previous expression. In fact, ++ A is the left value expression, so the ++ operation next to it is an error. When assigning values, the left side of the equal sign is not the left side.
Because it is a post ++ operator outside the brackets. Therefore, an error is returned.

Through the above tossing, we believe that we will have a deeper understanding of the abnormal expressions such as ++.


2. The differences between the front ++ and the back ++ of heavy load


I believe many people have done this operation. For the post ++, we need to add an int parameter. However, from the above analysis, note that the differences between the left and right values of the two are:
But to better meet the user's habits, we naturally want to return the left value expression for the front ++, and for the rear ++, the value to be returned is the left expression. Of course, the specific
Let's talk about it.


Frontend ++: If you define a Class A (containing a data member X), you must reload its frontend ++ operator, the first is to add the data member ++, and then return its reference.
In this case, the left value expression is used, so the following code: A & A: Operator ++ () {++ X; return * this;
} This method naturally expresses the role of the front-end, First calculation, in the return value.


Post ++: If you want to define post ++ in the preceding example, you need to pay attention to its right value. Does it return a reference or a class type? I have been thinking for a long time. Generally, the returned value is a reference, which is a left-value expression. If the returned value is a reference, it may cause problems in the code below.


The following code is displayed:

A A: Operator ++ (INT)


Return A (this-> X ++ );



In fact, I think this sentence is very good. It not only expresses the post meaning, but also successfully returns a value. The meaning of the post, that is, return first and then perform the auto-increment operation. Therefore, the default copy constructor is called to return a temporary variable.


3. Overload function parameters


In the past, I was not able to get entangled in this problem. Later I understood a little. Today I read a book and understood more. Now I understand the ancient saying, "I have read a book for a hundred times, the saying goes. It also understands the importance of reading.


For operator overloading, we often know that operators can be used as both member functions and common functions. In addition, different parameters are used as member functions, because in the class, so there are
A hidden this pointer. Therefore, you only need to write one binary operator, and the single-object operator does not need to be written. It is also an overload operator, which can be expressed in two ways, such as:
Operator + (A &, int value); // This is a common function A: Operator + (INT value)
; // This is a member function


For the two typical examples above, it is easy and straightforward to understand. When calling, write a + 5;
Where a is a type variable, so OK. However, if we think deeply that the + operator is overloaded, how can we differentiate the order of the two parameters for dual-purpose operations? Very simple. The operator above
Is the first operator, and the latter is the second operator. For a member function, pay attention to the role of this pointer, so it also shows the importance of the referenced object. Why am I so simple?
What else do you need to think about a single item? This is because it was hacked by an abnormal program in our book, as follows:


# Include <iostream> <br/> using namespace STD; <br/> struct a {<br/> int A; <br/> A (int x) {A = x ;}< br/>}; <br/> Class B {< br/> A X; <br/> public: <br/> A * operator-> (); <br/> B (INT V): X (v) {}< br/> }; <br/> A * B: Operator-> () {<br/> return & X; <br/>}< br/> int main () <br/>{< br/> B (5); <br/> int I = B-> A; <br/> B-> A = I + 5; <br/> I = (* B. operator-> ()). a; <br/>}< br/>


You can see that the truth is very simple, but it is heavy load->, and the heavy load is abnormal, it is a bit difficult to understand. For arrow operators, the left side is generally a pointer, but here
If it is overloaded, the left side is a Class Object. Note that the declaration is in B, so the left side is the B type variable, that is, B, and this is a single object operator, therefore, the pointer returned after the overload should be
Yes B->
-> A, because the pointer is returned after the operation on the left, and then access a, but it should be the compiler processing or specification here, so there is only one->, from here, I can see that
And the number of parameters. In the past, a parameter was always manually added or missing. Now we can calculate this into it and check whether it is a member. Then we can get a definite number.
To know the specific call sequence.

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.