c++ - void 函数中 return 的目的是什么

标签 c++

函数的最后一行放不放return有什么不同吗?

void InputClass::KeyDown(unsigned int input)
{
    // If a key is pressed then save that state in the key array
    m_keys[input] = true;
    return;
}

最佳答案

不,没有区别!

void 函数中的

return 用于在某些情况下提前退出。

例如:

void f(bool cond)
{
    // do stuff here
    if(cond)
        return;
    // do other stuff
}

关于c++ - void 函数中 return 的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638286/

相关文章:

C++ 插入运算符重载 (<<)

.net - C++ 中的 BSTR 和 SysAllockStringByteLen()

c++ - 传递 0 而不是 1 时 sfinae 模棱两可的调用

c++ - 如何从具有 null(0) 字符的 char 数组创建 C++ istringstream?

c++ - #include <comutil.h> 导致错误

c++ - C++读取不同类型的数据

c++ - 子类转换和指针地址更改

c++ - 如何通过 MFC 将应用程序设置保存在注册表中?

c++ - strlen 是否针对字符串文字进行了优化?

c++ - 函数原型(prototype)中的 const 参数