C++ 我可以在 void 函数中返回吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2249108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I return in void function?
提问by skydoor
I have to return to the previous level of the recursion. is the syntax like below right?
我必须返回到递归的前一个级别。像下面这样的语法对吗?
void f()
{
// some code here
//
return;
}
回答by Peter Alexander
Yes, you can return from a void function.
是的,您可以从 void 函数返回。
Interestingly, you can also return void from a void function. For example:
有趣的是,您还可以从 void 函数返回 void。例如:
void foo()
{
return void();
}
As expected, this is the same as a plain return;
. It may seem esoteric, but the reason is for template consistency:
正如预期的那样,这与普通的return;
. 这可能看起来很深奥,但原因是模板一致性:
template<class T>
T default_value()
{
return T();
}
Here, default_value
returns a default-constructed object of type T, and because of the ability to return void
, it works even when T
= void
.
在这里,default_value
返回一个默认构造的 T 类型对象,并且由于具有 return 的能力void
,即使在T
=时它也能工作void
。
回答by Ignacio Vazquez-Abrams
Sure. You just shouldn't be returning an actual value.
当然。您只是不应该返回实际值。
回答by icktoofay
Yes, you can use that code to return from the function. (I have to be very verbose here to make Stack Overflow not say that my answer is too short)
是的,您可以使用该代码从函数返回。(为了让 Stack Overflow 不说我的回答太短,这里我必须非常冗长)
回答by Casey
Yes, that will return from the function to the previous level of recursion. This is going to be very basic explanation, but when you call a function you are creating a new call stack. In a recursive function you are simply adding to that call stack. By returning from a function, whether you return a value or not, you are moving the stack pointer back to the previous function on the stack. It's sort of like a stack of plates. You keep putting plates on it, but than returning moves the top plate.
是的,这将从函数返回到上一级递归。这将是非常基本的解释,但是当您调用一个函数时,您正在创建一个新的调用堆栈。在递归函数中,您只是添加到该调用堆栈中。通过从函数返回,无论是否返回值,都将堆栈指针移回堆栈上的前一个函数。它有点像一堆盘子。你一直在上面放盘子,但返回移动顶盘。
You could also verify this by using a debugger. Just put a few break points in your code and step through it. You can verify yourself that it works.
您还可以使用调试器来验证这一点。只需在您的代码中放置几个断点并逐步执行即可。您可以自己验证它是否有效。
回答by Buhake Sindi
The simple answer to this is YES! C++ recognise void method as a function with no return. It basically tells the compiler that whatever happens, once you see the return;
break and leave the method....
对此的简单答案是肯定的!C++ 将 void 方法识别为没有返回值的函数。它基本上告诉编译器无论发生什么,一旦你看到return;
中断并离开方法......
回答by dromodel
As everyone else said, yes you can. In this example, return is not necessary and questionably serves a purpose. I think what you are referring to is an early return in the middle of a function. You can do that too however it is bad programming practice because it leads to complicated control flow (not single-entry single-exit), along with statements like break. Instead, just skip over the remainder of the function using conditionals like if/else().
正如其他人所说,是的,你可以。在这个例子中, return 是不必要的,而且有问题的目的。我认为您所指的是函数中间的早期返回。您也可以这样做,但是这是糟糕的编程实践,因为它会导致复杂的控制流(不是单入口单出口),以及诸如 break 之类的语句。相反,只需使用 if/else() 等条件跳过函数的其余部分。
回答by ThinkingInBits
Yes, sometimes you may wish to return void() instead of just nothing.
是的,有时您可能希望返回 void() 而不是什么都不返回。
Consider a void function that wants to call some pass-through void functions without a bunch of if-else.
考虑一个想要在没有一堆 if-else 的情况下调用一些 pass-through void 函数的 void 函数。
return
InputEvent == E_Pressed ? Controller->Grip() :
InputEvent == E_Released ? Controller->Release() :
InputEvent == E_Touched ? Controller->Touch() : void();
回答by Craig
You shouldn't have to have the return there, the program will return to the previous function by itself, go into debug mode and step through and you can see it yourself. On the other hand i don't think having a return there will harm the program at all.
您不必在那里返回,程序将自行返回上一个函数,进入调试模式并逐步执行,您可以自己看到它。另一方面,我认为在那里返回根本不会损害该计划。