c++ - 首先评估哪个默认参数,为什么?

标签 c++

我有三个函数,funt1()funt2()funt3()

int funt1()
{
    cout<<"funt1 called"<<endl;
    return 10;
}

int funt2()
{
    cout<<"funt2 called"<<endl;
    return 20;
}

void funt3(int x=funt1(), int y=funt2())
{
    cout << x << y << endl;
}

我的主要函数:

int main()
{
    funt3();
    return 0;
}

当我在 main() 方法中调用 funt3() 时,为什么先调用 funt1(),然后 funt2()?

最佳答案

这取决于您的编译器。其他人可能会先调用 funct2()。 C 或 C++ 都不能保证函数参数的计算顺序。

参见 Parameter evaluation order before a function calling in C

关于c++ - 首先评估哪个默认参数,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7250150/

相关文章:

c++ - C++中的变量存储在哪里?

gdal - 如何使用 visual studio 在 QT 中制作 GUI?

c++ - 无法将参数 'char*(*)[6]' 的 'char***' 转换为 '2' 到 'void prac(int*,char***)'

c++ - 获取 C# 输出字符串参数值到 C++ BSTR*?

c++ - static_assert'ion long 和 int 是同一类型

C++ 使用一种方法且没有警告地实现不同类型赋值的优雅方法?

c++ - const 对象的代码大小

c++ - C++ 基础知识与 Arduino

c++ - char 缓冲区初始化的标准行为

c++ - 在 visual studio 和 eclipse 中打印 wchar_t 会给出不同的结果