返回类型vs传递引用

编程入门 行业动态 更新时间:2024-10-23 19:25:04
本文介绍了返回类型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> 写道: On Sun, 16 Nov 2008 09:48:02 +1300, Ian Collins <ia******@hotmail> wrote: > Carl Forsman写道: >Carl Forsman wrote: >然后我可以让代码更干净,让所有功能无效返回类型,如果我需要一个函数的返回值,我只需传入我希望函数返回的指针(例如* SYSTEMTIME时间) >Then I can make code more clean to have all function to void returntype and if I need a return value from a function, I just pass in thepointer 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 wherethe parameter types are used to resolve the argument types.Else where, it just leads to clutter. You have to use variableswhenever you call a function, whether you want to retain the result ornot. so rather than simply writingint n = doSomething();you end up withint n;doSomething( n );manageable for simple variables, but useless for constants or objectssuch 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> 写道: On Sun, 16 Nov 2008 09:48:02 +1300, Ian Collins <ia******@hotmail> wrote: > Carl Forsman写道: >Carl Forsman wrote: >>然后我可以让代码更干净,让所有函数无效返回类型,如果我需要函数的返回值,我只需传入我希望函数返回的指针(例如* SYSTEMTIME时间) >>Then I can make code more clean to have all function to void returntype and if I need a return value from a function, I just pass in thepointer 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 wherethe parameter types are used to resolve the argument types.Else where, it just leads to clutter. You have to use variableswhenever you call a function, whether you want to retain the result ornot. so rather than simply writingint n = doSomething();you end up withint n;doSomething( n );manageable for simple variables, but useless for constants or objectssuch 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传递引用

本文发布于:2023-10-16 11:56:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!