Detailed description of the use of the C ++ comma Operator

Source: Internet
Author: User

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:

 
 
  1. class mynum  
  2. {  
  3. public:  
  4. mynum(double ndb,...)  
  5. {  
  6. //init with arguments  
  7. }  
  8. };  
  9. class someclass  
  10. {  
  11. someclass():  
  12. num(3,4)  
  13. ,a(0)  
  14. ,b(0)  
  15. ,c(15)  
  16. {  
  17. int i,j;   
  18. i=1,2; //int x=1,2;  
  19. for(;b<10;++b,--c)  
  20. {  
  21. ++a;  
  22. }  
  23. }  
  24. mynum num;  
  25. int a;  
  26. int b,c;  
  27. }; 

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 !)

 
 
  1. int i,j;  
  2. i=1,2;  
  3. 3,4;  
  4. j=(1,2);  
  5. 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:

 
 
  1. int myadd(int i,int j,int k)  
  2. {  
  3. return i+j+k;  
  4. }  
  5. int myadd(int i,int j)  
  6. {  
  7. return i+j;  
  8. }  
  9. int main()  
  10. {  
  11. for(int x=0;x<9;++x)  
  12. {  
  13. printf("%d",myadd((2,3),4));  
  14. }  

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!

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.