返回void函数的值 [英] return values for void functions

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

问题描述

前几天我做了类似的事情:


void foo

{

}


void bar()

{

return foo();

}


当我打算这样做时:


void foo

{

}


void bar()

{

foo();

return;

}


我很惊讶它有效。这是合法的吗?


谢谢。

The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?

Thanks.

推荐答案

REH写道:
前几天我做了类似的事情:

void foo
{
}

void bar()
{
返回foo();
}

当我打算这样做时:

void foo
{
}

void bar()
{
foo();
返回;
}

我很惊讶它有效。这是合法的吗?
The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?




是的。


V



Yes.

V


这很重要,因为如果你有一个系列if if statments。你满足条件时,
可以退出这个功能。还有

void func(int& a,int& b,char& c){}

That is important because if you have a series if if statments. You
can get out of the function when your condition is met. There is also
void func(int &a, int &b, char &c){}


REH写道:
前几天我做了类似的事情:

void foo
{
}

()
{
返回foo();
}
当我打算这样做时:

void foo
{
}

空栏()
{
foo();
返回;
}

我很惊讶它有效。这是合法的吗?

谢谢。
The other day I did something like this:

void foo
{
}

void bar()
{
return foo();
}

when I meant to do this:

void foo
{
}

void bar()
{
foo();
return;
}

I was suprised that it worked. Is this legal?

Thanks.




是的,是的,它可以让你涵盖一个功能的案例/>
返回void,代码如下:


template< typename xResult>

xResult foo(xResult(* fFunc)())

{

return(* fFunc)();

}


而不是让你写一个特殊的案例来处理

返回void的函数,例如:


void foo(void(* fFunc)())

{

* fFunc();

}


必须写第二个案例以涵盖返回类型的空白将是

a主要的痛苦。



Yes it is, and the fact that it is lets you cover the case of a function
returning void with code like:

template < typename xResult >
xResult foo(xResult (*fFunc)())
{
return (*fFunc)();
}

instead of making you write a special case that handls functions that
return void, for example:

void foo(void (*fFunc)())
{
*fFunc();
}

Having to write that second case to cover a return type of void would be
a major pain.


这篇关于返回void函数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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