The comma operator is a very common operator in program development. Are there any good operators in C ++? If so, how should we use the C ++ comma operator correctly? Here we will give you a detailed introduction to related concepts.
Many C ++ newbie will ask this question. Everyone knows +-*/and % of others! & | Or something like that, but the comma operator? Maybe half of them will not know what it is.
In fact, we often use the comma operator, but not all the commas in the Code are comma operators.
Let's start with the sample code of a class:
- class mynum
- {
- public:
- mynum(double ndb,...)
- {
- //init with arguments
- }
- };
- class someclass
- {
- someclass():
- num(3,4)
- ,a(0)
- ,b(0)
- ,c(15)
- {
- int i,j;
- i=1,2; //int x=1,2;
- for(;b<10;++b,--c)
- {
- ++a;
- }
- }
- mynum num;
- int a;
- int b,c;
- };
In this Code, the comma appears in lines 5, 13-16, 18-20, and 27.
- Detailed description of C ++ attributes
- C ++ exception throwing skills
- C ++ new usage experience
- Introduction to the correct use of C ++ inline functions
- Introduction to unsuitable use of C ++ inline functions
Now let's take a look at what the C ++ comma Operator means:
A comma-containing expression first calculates the expression on the left of the comma and then the expression on the right of the comma. The result of the entire expression is the value of the expression on the right of the comma.
Then let's look at the above Code:
The second row is a comma in the constructor of the mynum class. It is only used to separate the first double type parameter from the variable parameter. This is a special case. If 3.4 is used as the first parameter when constructing a mynum object, and the decimal point is accidentally changed to a comma, 3.4 is changed to two parameters 3 and 4 -- for example, in the case of 13th rows. In this case, the compiler will not report errors and the program can run as well. However, the member variables in the mynum class may be completely different because of the variable values passed in by the constructor. Neither of the two commas is a comma operator.
The comma in lines 14th, 15, and 16 is not a comma operator. The three commas are only used to separate the initialization list of member variables of the constructor.
The int I, j; of the 18th line is a statement commonly used for variable declaration. Here, the comma serves only as an interval variable declaration, not a comma operator.
If you do not know the C ++ comma operator, you must think it is wrong! In fact, this is the comma operator. Let's see, after I =; what is the value of I? According to the definition of the comma operator, we can easily obtain I = 2. However, this answer is wrong! When you get the wrong answer, you ignore an important factor-operator priority! Because = has a higher priority, I = 1, 2; this statement is equivalent to (I = 1), 2; if you want to get the expected 2, we should write I = (1, 2 );. The following code can verify this statement. Note that the first line is meaningless code, but it is not an error code !)
- int i,j;
- i=1,2;
- 3,4;
- j=(1,2);
- printf("%d\n%d\n",i,j);
Continue to read the first line of code, which is a for loop code. The comma here is also a comma operator. In fact, for loop is also a frequent occurrence of comma operators. Only one expression can be written in the for loop, while the comma expression allows you to complete the calculation of two or more expressions. For example, "for (; B <10; ++ B, -- c)", ++ B and -- c are executed in each loop.
There is no need to say more about 27th rows. That is, a common variable declaration statement. Here, the comma is not the C ++ comma operator.
In general, the comma operator is not a very common operator. It only works well in some specific context environments. At the same time, you must be very careful when encoding the incorrect input comma to cause the error.
For example, commas and parentheses may cause some potential problems. If we neglect coding, We will generate some hidden bugs that are hard to trace. We only need to abide by some established coding specifications, developing good and rigorous coding habits can help avoid such problems. Take a look at the following code:
- int myadd(int i,int j,int k)
- {
- return i+j+k;
- }
- int myadd(int i,int j)
- {
- return i+j;
- }
- int main()
- {
- for(int x=0;x<9;++x)
- {
- printf("%d",myadd((2,3),4));
- }
- }
Because of the fact that the brackets are incorrectly added in the actual encoding in Row 3, this is too common), the result is that the entire output is completely wrong! Because (2, 3) the return value of the C ++ comma operator expression is 3, 15 rows are equivalent to myadd (3, 4!