How to determine C++ function call order?

后端 未结 3 1451
予麋鹿
予麋鹿 2021-01-23 05:11

I\'ll skip the headers

class X {
    int i;
    static int j;

public:
    X(int ii = 1) : i(ii) {
        j = i;
    }
    static int incr() {
        return ++         


        
3条回答
  •  我在风中等你
    2021-01-23 05:37

    This statement

    cout << x.f() << xp->f() << X::f();
    

    can be rewritten like

    cout.operator <<( x.f() ).operator <<( xp->f() ).operator <<( X::f() );
    

    The order of evaluations of expressions used as arguments in this statement is unspecified. So the compiler can evaluates them for example from left to right or from right to left. Thus using different compilers you can get different results.:)

    To get a deterministic result you should rewrite statement

    cout << x.f() << xp->f() << X::f();
    

    for example like

    cout << x.f();
    cout << xp->f();
    coit << X::f();
    

提交回复
热议问题