After reading C traps and pitfalls for a few hours last night, I felt that all the things mentioned here were marginal things. I still had a limited level and could not understand them, however, I am very interested in one of the questions:
Exercise 1-4. What is the meaning of a ++ B?
In fact, the answer is very simple. (A ++) + B. Later, when I read the answer, I found the following sentence: this syntax is incorrect, the result of a ++ cannot be left.
For ++ operations, prefixes, suffixes, and other people talk a lot about it, but there is little talk about the left value, so I decided to give a look at this issue.
First, let's look at what is the left value.
Left value (lvalue): A left value expression is a reference to a variable storage expression. A variable storage expression is called a left value expression. We can simply understand it as an expression that can be assigned a value.
Let's take a look at this.Program:
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Int A = 1;
(A ++) ++;
++ (++ );
(++ A) ++;
++ (A ++ );
Return 0;
}
Error c2105: '+ +' needs L-value cannot be passed during compilation. That is to say, the ++ operator lacks a value to participate in ++ operations, we know that the ++ operator is a single object operator. It lacks a value to participate in this operation, and of course it cannot be compiled. Through compilation, we can also find out the problem
(A ++) ++; and ++ (A ++. This is because the value returned by ++ A is the left value, while that returned by a ++ is not the left value. The cause is the definition of incremental operations in C/C ++:
Return reference:
Type & type: Operator ++ ()
{
This. Value + = 1;
Return * this;
}
Return Value:
Type & type: Operator ++ ()
{
Type Co (this); this. Value + = 1;
Return Co;
}
During the operation, the object is first modified and then the operation of the returned object is called the pre-increment operator. In the operator overload, the return object reference method is adopted. Otherwise, it is called the post-increment operator, return object values in Operator overloading. The referenced function can be returned as the left value.
Now let's do a test: define a function max (int x, int y); to see what is the difference between the returned value and the returned reference:
# Include <iostream>
Using namespace STD;
Int & MAX (int x, int y );
Void main ()
{
Int temp = 0;
Temp = max (2, 3) = 4;
Cout <temp <Endl;
}
Int & MAX (int x, int y)
{
Return x> Y? X: Y;
}
In this program, we define a max function, which returns a relatively large one of the two integer numbers, but here is a sentence that is hard to understand: temp = max (2, 3) = 4; in fact, this means that max () returns a reference and can be assigned a value. Now we change this function to the return value:
# Include <iostream>
Using namespace STD;
Int max (int x, int y );
Void main ()
{
Int temp = 0;
Temp = max (2, 3) = 4;
Cout <temp <Endl;
}
Int max (int x, int y)
{
Return x> Y? X: Y;
}
It cannot be compiled now: Error c2106: '=': left operand must be L-Value
References:
Andrew Koening. c traps and pitfalls. People's post and telecommunications press. 2002.13, 129
Scott Meyers. effctive C ++. Huazhong University of Science and Technology Publishing House. 2003. 64-68