写 void 函数的方式不同 return
Difference in a way of writing a void function return
这是我找到的代码片段 here
void App::onSave()
{
if (filename.isEmpty())
return onSaveAs();
//do saving here
}
void App::onSaveAs()
{
QString f = QFileDialog::getSaveFileName(NULL, "Save as", "", "*.sb");
if (!f.isEmpty())
{
//filename generation
return onSave();
}
}
我的问题是:return onSave();
和像这样写的 return onSave();
之间有什么区别吗:
onSave();
return;
这让我很困惑,因为这些函数是 void 类型,没有 return 任何东西。
您问的是:
is there any difference between
return onSave();
and
onSave();
return;
两者没有区别
C++ 标准说:
A return statement with an expression of type void can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.
所以这两种形式在C++中是等价的
注意 C 标准说:
A return statement with an expression shall not appear in a function whose return type is void
在 C++ 中放宽此限制可能与模板有关。
这是我找到的代码片段 here
void App::onSave()
{
if (filename.isEmpty())
return onSaveAs();
//do saving here
}
void App::onSaveAs()
{
QString f = QFileDialog::getSaveFileName(NULL, "Save as", "", "*.sb");
if (!f.isEmpty())
{
//filename generation
return onSave();
}
}
我的问题是:return onSave();
和像这样写的 return onSave();
之间有什么区别吗:
onSave();
return;
这让我很困惑,因为这些函数是 void 类型,没有 return 任何东西。
您问的是:
is there any difference between
return onSave();
and
onSave(); return;
两者没有区别
C++ 标准说:
A return statement with an expression of type void can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.
所以这两种形式在C++中是等价的
注意 C 标准说:
A return statement with an expression shall not appear in a function whose return type is void
在 C++ 中放宽此限制可能与模板有关。