How to determine C++ function call order?

后端 未结 3 1409
予麋鹿
予麋鹿 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:43

    The stream operator is simply an operator. C++ allows operators to be defined a multitude of ways. What you're seeing is the << operator for strings. What you're doing is the same thing as:

    cout << "word1" << "word2" << "word3";
    

    The expect output for that is:

    "word1word2word3"
    

    However, the evaluation order is determined by the implementation of the << operator. Usually the above is equivalent to:

       (((cout << "word1") << "word2") << "word3");
    

    Replace the strings with functions instead and you see the first one evaluated is actually "word3". This is not strictly the case, just what you'll usually see.

       (((cout << func1()) << func2()) << func3());
    

    Evaluation order would be func3, func2, func1, with print order the return of func1, func2, func3.

提交回复
热议问题