返回类型vs传递引用 [英] return type vs passing a reference

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

问题描述

有两种方法可以从函数返回值

====================

1)传递引用作为参数 - 以下将返回时间


void Table :: Get(char * FieldName,* SYSTEMTIME time)

{

_variant_t vtValue;

vtValue = m_Rec-> Fields-> GetItem(FieldName) - > GetValue();


if(vtValue.vt == VT_NULL){

返回NULL;

}

VariantTimeToSystemTimeWithMilliseconds(vtValue.date,time);

}

====================

2)使用return keyword

SYSTEMTIME表:: Get(char * FieldName)

{

_variant_t vtValue;

vtValue = m_Rec-> Fields - > GetItem(FieldName) - > GetValue();


if(vtValue.vt == VT_NULL){

返回NULL;

}

SYSTEMTIME m_st;

VariantTimeToSystemTimeWithMilliseconds(vtValue.date,& m_st);

返回m_st;

}


====================


我是否只能使用第一种方式从a返回值功能?因为

两种方式都做同样的事情来返回一个值。


然后我可以让代码更干净,让所有函数都无效返回

类型,如果我需要函数的返回值,我只需传入

指针,我希望函数返回(例如* SYSTEMTIME时间)

there are 2 ways to return a value from a function
====================
1) passing a reference as parameter - the following will return time

void Table::Get(char* FieldName, *SYSTEMTIME time)
{
_variant_t vtValue;
vtValue = m_Rec->Fields->GetItem(FieldName)->GetValue();

if (vtValue.vt == VT_NULL) {
return NULL;
}
VariantTimeToSystemTimeWithMilliseconds (vtValue.date, time);
}
====================
2) use return keyword
SYSTEMTIME Table::Get(char* FieldName)
{
_variant_t vtValue;
vtValue = m_Rec->Fields->GetItem(FieldName)->GetValue();

if (vtValue.vt == VT_NULL) {
return NULL;
}
SYSTEMTIME m_st;
VariantTimeToSystemTimeWithMilliseconds (vtValue.date, &m_st);
return m_st;
}

====================

Can I only use the 1st way to returning value from a function? as
both ways is doing the same thing to return a value.

Then I can make code more clean to have all function to void return
type and if I need a return value from a function, I just pass in the
pointer that I want the function to return (e.g. *SYSTEMTIME time)

推荐答案

Carl Forsman写道:
Carl Forsman wrote:

然后我可以让代码更干净,让所有函数都无效返回

类型,如果我需要函数的返回值,我只需传入

指针,我希望函数返回(例如* SYSTEMTIME时间)
Then I can make code more clean to have all function to void return
type and if I need a return value from a function, I just pass in the
pointer that I want the function to return (e.g. *SYSTEMTIME time)



你可以这样做,但我怀疑很多人会同意你对清洁的定义。


做你所描述的最常用的是函数模板

参数类型用于解析参数类型。


否则,它只会导致混乱。每当你调用一个函数时,你必须使用变量

,无论你是想保留结果还是

都没有。所以而不是简单地写作


int n = doSomething();


你最终得到


int n;

doSomething(n);


可管理简单变量,但对于常量或对象无用

这样作为需要初始化的std :: auto_ptr。


-

Ian Collins

You can do this, but I doubt many would agree with your definition of clean.

Doing what you describe is most often used for function templates where
the parameter types are used to resolve the argument types.

Else where, it just leads to clutter. You have to use variables
whenever you call a function, whether you want to retain the result or
not. so rather than simply writing

int n = doSomething();

you end up with

int n;
doSomething( n );

manageable for simple variables, but useless for constants or objects
such as std::auto_ptr that require initialisation.

--
Ian Collins


在Sun上,2008年11月16日09:48:02 +1300,Ian Collins< ia ****** @ hotmail.com>

写道:
On Sun, 16 Nov 2008 09:48:02 +1300, Ian Collins <ia******@hotmail.com>
wrote:

> Carl Forsman写道:
>Carl Forsman wrote:

>然后我可以让代码更干净,让所有功能无效返回
类型,如果我需要一个函数的返回值,我只需传入我希望函数返回的指针(例如* SYSTEMTIME时间)
>Then I can make code more clean to have all function to void return
type and if I need a return value from a function, I just pass in the
pointer that I want the function to return (e.g. *SYSTEMTIME time)


你可以这样做,但是我怀疑很多人会同意你对清洁的定义。

做你所描述的最常用于功能模板测试中使用参数类型来解析参数类型。

否则,它只会导致混乱。无论何时调用函数,无论是想保留结果还是不保留结果,都必须使用变量
。所以而不是简单地写作

int n = doSomething();

你最终得到

int n;
doSomething(n );

可管理简单变量,但对常量或对象无用,例如需要初始化的std :: auto_ptr。


You can do this, but I doubt many would agree with your definition of clean.

Doing what you describe is most often used for function templates where
the parameter types are used to resolve the argument types.

Else where, it just leads to clutter. You have to use variables
whenever you call a function, whether you want to retain the result or
not. so rather than simply writing

int n = doSomething();

you end up with

int n;
doSomething( n );

manageable for simple variables, but useless for constants or objects
such as std::auto_ptr that require initialisation.



所以你的意思是我更好地使用Way#2 - (返回值)当我想要返回

某个正常的东西时方式?

so you mean I better use Way #2 - (return value) when I want to return
something in a "normal" way?


Carl Forsman写道:
Carl Forsman wrote:

On Sun,2008年11月16日09:48:02 +1300 ,Ian Collins< ia ****** @ hotmail.com>

写道:
On Sun, 16 Nov 2008 09:48:02 +1300, Ian Collins <ia******@hotmail.com>
wrote:

> Carl Forsman写道:
>Carl Forsman wrote:

>>然后我可以让代码更干净,让所有函数无效返回
类型,如果我需要函数的返回值,我只需传入我希望函数返回的
指针(例如* SYSTEMTIME时间)
>>Then I can make code more clean to have all function to void return
type and if I need a return value from a function, I just pass in the
pointer that I want the function to return (e.g. *SYSTEMTIME time)


你可以这样做,但我怀疑很多人会同意你的清洁定义。

你所描述的内容最常用于功能模板,其中参数类型用于解析参数类型。

否则,它只是引导杂乱无章。无论何时调用函数,无论是想保留结果还是不保留结果,都必须使用变量
。所以而不是简单地写作

int n = doSomething();

你最终得到

int n;
doSomething(n );

可管理简单变量,但对常量或对象无用,例如需要初始化的std :: auto_ptr。

You can do this, but I doubt many would agree with your definition of clean.

Doing what you describe is most often used for function templates where
the parameter types are used to resolve the argument types.

Else where, it just leads to clutter. You have to use variables
whenever you call a function, whether you want to retain the result or
not. so rather than simply writing

int n = doSomething();

you end up with

int n;
doSomething( n );

manageable for simple variables, but useless for constants or objects
such as std::auto_ptr that require initialisation.



所以你的意思是我更好地使用Way#2 - (返回值)当我想要返回

某个正常的东西时办法?


so you mean I better use Way #2 - (return value) when I want to return
something in a "normal" way?



除了功能模板,是的。


-

Ian Collins

With the exception of function templates, yes.

--
Ian Collins


这篇关于返回类型vs传递引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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