如何退出C ++中的void函数? [英] How do you exit from a void function in C++?

查看:371
本文介绍了如何退出C ++中的void函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它是一个void函数,你如何提前退出一个函数?我有一个void方法,如果某个条件为真,则不需要执行它的代码。

How can you prematurely exit from a function if it is a void function? I have a void method that needs to not execute its code if a certain condition is true. I really don't want to have to change the method to actually return a value.

推荐答案

使用返回语句!

return;

if (condition) return;

您不需要(且不能)指定任何值, code> void 。

You don't need to (and can't) specify any values, if your method returns void.

这篇关于如何退出C ++中的void函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆