反向嵌入函数 运行 中的 C++ 函数?

C++ functions embedded in functions run in reverse?

抱歉,如果这看起来像一个愚蠢的问题。我相信你们知道为什么会这样,但是在我的 (C++) 函数代码中

int result = calculateResult(getUserInput1(), getMathematicalOperation() , getUserInput2())

函数 'getUserInput2()' 首先是 运行 而不是 'getUserInput1()。当嵌入另一个函数时,C++ 中的函数向后 运行 正常吗?

完整代码如下

// SimpleCalculator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

int getUserInput1()
{
    std::cout << "Please enter an integer 1: ";
    int value;
    std::cin >> value;
    return value;
}

int getUserInput2()
{
    std::cout << "Please enter an integer 2: ";
    int value;
    std::cin >> value;
    return value;
}

int getMathematicalOperation()
{
    std::cout << "Please enter the operator you wish to use (1 = +, 2 = -, 3 = *, 4 = /): ";
    int op;
    std::cin >> op;

    std::cout << "the operator number you chose is : " << std::endl << op << std::endl;

    return op;
}

int calculateResult(int x, int op, int y)
{
    if (op == 1)
        return x + y;
    if (op == 2)
        return x - y;
    if (op == 3)
        return x * y;
    if (op == 4)
        return x / y;

    return -1;
}

void printResult(int result)
{
    std::cout << "Your result is : " << result << std::endl;
}

是的,很正常

严格来说,评估顺序是未指定,这是该规则的常见结果。

这可能看起来有点愚蠢,但我认为这很好,因为它确实强调了您不应该依赖任何特定的顺序,尤其是您最初可能假设的所有最左优先顺序。

如果您需要特定的评估顺序,请先将调用结果存储在变量中。