函数缺少返回值,运行时行为

正如预期的那样,编译器(VisualStudio 2008)将发出警告


警告C4715:“ doSomethingWith”:并非所有控制路径都返回值


编译以下代码时:


int doSomethingWith(int value)

{

    int returnValue = 3;

    bool condition = false;


    if(condition)

        // returnValue += value; // DOH


    return returnValue;

}


int main(int argc, char* argv[])

{

    int foo = 10;

    int result = doSomethingWith(foo);

    return 0;

}

但是程序运行正常。doSomethingWith()函数的返回值为0。


是只是未定义的行为,还是在运行时确定/如何计算结果值有特定规则。非POD数据类型作为返回值会怎样?


摇曳的蔷薇
浏览 339回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP