The analysis of the 39th class comma operator

Source: Internet
Author: User

1. comma operator

(1) A comma expression is used to concatenate multiple sub-expressions into an expression

(2) The value of the comma expression is the value of the last sub-expression

(3) The first N-1 subexpression of a comma expression can have no return value

(4) The comma expression evaluates the value of each sub-expression in the left-to-right order

Exp1,exp2,exp3,...,ExpN;

Example of an instance analysis comma expression

#include <iostream>using namespacestd;voidFuncinti) {cout<<"func (): i ="<< I <<Endl;}intMain () {inta[3][3] = {        (0,1,2),//Note that this is a comma, not a curly brace.(3,4,5),//equivalent to assigning values to only the first three elements of the array(6,7,8)//They are 2, 5, 8, the rest are 0 .    }; inti =0; intj =0;  while(I <5) func (i),//This combines an expression with the i++ of the next linei++;//so the while does not die loop         for(intI=0; i<3; i++)    {         for(intj=0; j<3; J + +) {cout<< A[i][j] << Endl;//2,5,8,0,0,0,0,0,0}} (I, j)=6;//valid: Comma expression returns J, equivalent to J = 6cout<<"i ="<< i << Endl;//5cout <<"j ="<< J << Endl;//6        return 0;}

2. overloading the comma operator

(1) overloading the comma operator in C + + is legal

(2) overloading the comma operator with a global function

(3) The parameters of an overloaded function must have a class type (because this refers to the operator overload of the class)

(4) The return value type of an overloaded function must be a reference

classtype& operator , (constconst classtype& b) {    return//  return right operator, must cast  }

"Programming Experiment" overloaded comma operator

#include <iostream>using namespacestd;classtest{intMvalue; Public: Test (inti) {Mvalue =i;} intValue () {returnmvalue;}};//using global variables to overload the comma operator//1. Parameters must have one for class type//2. The return value must be a referencetest&operator,(Consttest& A,Consttest&b) {    returnConst_cast<test&> (b);//Forced Conversions}test func (Test&i) {cout<<"func (): i ="<< I.value () <<Endl; returni;}intMain () {Test T0 (0); Test T1 (1); //because the nature of the overloaded operator is equivalent to a function call,//equivalent to test TT = operator, (func (t0 (0)), func (T1 (1)));//function Call! //when entering the function body, the value of the parameter must be computed, while the two parameters func (t0 (0)) and func (T1 (1) )//The order of calculation is indeterminate, under g++ compilation, the output//func (): i = 1//func (): i = 0Test TT = (func (t0), func (T1));//comma expression, intended to be calculated from left to rightcout<< Tt.value () <<endl;//1, although the result is correct, but the middle of the computational process with//the literal meaning of the comma expression is calculated from left to right and may not be consistent.         return 0;}

3. problems and analysis during overloading

(1) C + + function call extension operator function

(2) The calculation of all parameters must be completed before entering the function body

(3) The order of calculation of function parameters is variable

(4) overload cannot strictly calculate the expression from left to right , return can be left to right without overloading, so the overloaded comma operator is completely meaningless .

4. Summary

(1) Comma expression evaluates the value of each subexpression from left-to- right order

(2) The value of the comma expression is the value of the last sub-expression

(3) Operator overloading cannot fully implement the native meaning of the comma operator (from left to right)

(4) do not overload the comma operator in engineering development

The analysis of the 39th class comma operator

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.