C ++ functions pass value problem

Emergence of a magical thing to do title, by value of C ++ with other OOP languages ​​are not the same. First do a test to see what the output Below are?

void F(int a,int b,int c){
    cout<<a<<b<<c;
}
int main ()
{
    int a=1;
    F(++a,a++,++a);
}

I guess most think it should be 2,2,4. C # is indeed the result. C ++ but the result is not the case. Check the information and asking friends to answer very complex, specific mechanisms which are not clear. Only know C ++ function by value may be based on the stack.

He began to push from the left, to the right of the stack, as opposed to C #. There are different opinions welcomed the guidance of the great God

If the individual function that starts from the output value on the right, such as a ++, the value of a first output, when the output value of a becomes 2, and if a ++, 2 becomes re-output. However, if both output a ++, ++ a, ++ a is to a pressure in the bottom of the stack, i.e. all

++ a value of the output at the last and the same, the following examples:

    // convenience are located a 1
    / * Output 424 
       process because of the rightmost ++ symbol pressed against a front bottom of the stack and at this time becomes the value of 2
       to as intermediate ++ symbol after the output value after this time a 2 a 3 
       Last leftmost Thus the front ++ symbol has a stack 4 at this time becomes a 
       final end of the traverse both a unstack a output * / 
    F. (+ a, + a, + a);
     // the following examples prove such
     / / output 321 
    F. (A ++, A ++, A ++ );
     // output 344 
    F. (A ++, A ++, ++ A);
     // output 444 
    F. (++ A, A ++, ++ A) ;
     // output 222 
    F. (A, A, ++ A);
     // output 421 
    F. (++ A, A ++, A ++);

So if it is the case of multiple numbers? Can be seen separately, A look at a, b or sequence see b above, but met a + b where, at that point they present output value, as follows:


// set A =. 1 B =. 3
//
output. 1. 3. 4 F. (A ++, B ++, A + B); // output. 6. 1. 3 F. (A + B, A ++, B ++ ); // output 83 . 3. 1. 5 F. (A + B, A ++, B ++, A ++, B ++ ); // output. 3. 5. 6. 4 2 F. (A ++, B ++, A + B, A ++, B ++);

If a supplement is cout << output according to the output of the last comma expression can be as follows:

    int a=1,b=3;
    //输出4
    cout<<(a++,b++,a+b,++a,b++);
    //输出5    
    cout<<(a++,b++,a+b,++a,++b);

 

 

Guess you like

Origin www.cnblogs.com/dlvguo/p/11902274.html