返回参考 [英] returning references

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

问题描述

以下是斯坦福大学学生电脑链接发布的

科学。


语录开始于此

由于滥用的风险,一些专家建议永远不要返回

a

来自某项功能或方法的参考。

引用此处结束

我从来没有听说过其他人说这是一个函数

返回引用的问题。是否真的有专家反对,或者只是普通的观察参考 -

返回是一个需要学习的有点困难的概念


小心点? (我现在正在学习这个。)假设程序员

有一定的能力,并且能够避免返回

对当地人的引用等等,什么(如果有的话) )是否存在陷阱?


Paul Epstein

解决方案

1月3日下午6:26,pauldepst ... @ att.net写道:


以下内容来自斯坦福大学学生的电脑链接

science。


报价从此开始

由于存在滥用风险,一些专家建议永远不要退货

a

来自功能或方法。

报价结束这里


我从来没有听过有人说这是一个功能问题

返回参考。是否真的有专家反对,或者只是普通的观察参考 -

返回是一个需要学习的有点困难的概念


小心点? (我现在正在学习这个。)假设程序员

有一定的能力,并且能够避免返回

对当地人的引用等等,什么(如果有的话) )是陷阱吗?


保罗爱泼斯坦



我没有看到任何返回引用的内容

const关键字在适当时正确使用。令人惊讶的是,一个好的

人在创建它们的函数存根时不使用const;


const MyClass& GetMyClass()const; //典型的访问者

SetMyClass(const MyClass& myclass); //典型的mutator


我在调试时经常发现以上两种方法在

源代码中缺少const关键字,导致其他程序员误用

简单地看一眼方法而不考虑影响

对返回的参考进行更改。


另一个常见的我遇到的问题是你不能为

引用返回NULL。有时很高兴有选项返回NULL

发生错误在这种情况下我使用指针并告诉调用者

检查返回值以确定是否发生错误。有些人虽然不喜欢这个想法,但值得一提。


2008-01-04 01:26, pa**********@att.net 写道:
< blockquote class =post_quotes>
以下内容来自斯坦福大学学生的电脑链接

science。


语录开始于此

由于存在滥用的风险,一些专家建议永远不要返回

a

函数或方法的参考。

报价结束HERE


我从来没有听过其他人说这是一个函数的问题

来返回一个引用。是否真的有专家反对,或者只是普通的观察参考 -

返回是一个需要学习的有点困难的概念


小心点? (我现在正在学习这个。)假设程序员

有一定的能力,并且能够避免返回

对当地人的引用等等,什么(如果有的话) )是陷阱吗?



据我所知,只有一个陷阱,那就是返回一个对函数本地变量的

引用,比如所以:


int& foo(int i)

{

int r = i + i;

return r;

}


int main()

{

int&两次= foo(5);

}


由于r是foo()的局部变量,因此只要

foo()完成执行,这意味着返回的引用将

引用到不再存在的变量。


另一方面手中有一些有效的理由从函数返回一个

引用,一个从

集合返回元素的函数就是一个很好的例子(看看at ()函数在std :: vector中。


-

Erik Wikstr ?? m


< blockquote> 1月4日,12:26 * am,pauldepst ... @ att.net写道:


以下是来自斯坦福大学学生计算机的链接< br $> b $ b科学。


语录在此处开始

由于存在误用的风险,一些专家建议永远不要返回

a

函数或方法的参考。

报价结束此处



hmmm ...我认为遵循这个非常明智和有用的建议,

斯坦福大学的学生将不被允许做...


#include< iostream>

int main(){

std :: cout<< hello world \ n;

}

问候

Andy Little


(谢天谢地,不是C ++专家!):-)


Below is posted from a link for Stanford students in computer
science.

QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or method.
QUOTE ENDS HERE

I have never heard anyone else say that it is a problem for a function
to return a reference. Are there really experts who object, or is
this nothing other than the commonplace observation that reference-
returning is a somewhat difficult concept that needs to be learned
carefully? (I am just learning about this now.) Assuming programmers
have some degree of competence and are able to avoid returning
references to locals and so on, what (if anything) are the pitfalls?

Paul Epstein

解决方案

On Jan 3, 6:26 pm, pauldepst...@att.net wrote:

Below is posted from a link for Stanford students in computer
science.

QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or method.
QUOTE ENDS HERE

I have never heard anyone else say that it is a problem for a function
to return a reference. Are there really experts who object, or is
this nothing other than the commonplace observation that reference-
returning is a somewhat difficult concept that needs to be learned
carefully? (I am just learning about this now.) Assuming programmers
have some degree of competence and are able to avoid returning
references to locals and so on, what (if anything) are the pitfalls?

Paul Epstein

I don''t see anything at all with returning references as long as the
const keyword is used correctly when appropriate. Surprisingly, a good
number of people do not use const when creating thier function stubs;

const MyClass & GetMyClass() const; // The typical accessor
SetMyClass(const MyClass & myclass); // The typical mutator

I constantly find the above two methods lacking the const keyword in
source when I am debugging and it leads to misuse by other programmers
who simply glance at a method without taking into account the impact
of making changes to the returned reference.

Another common problem I run into is you can''t return NULL for a
reference. Sometimes it is nice to have the option to return NULL when
an error occured in which case I use a pointer and tell the caller to
check the returned value to determine if an error occured. Some people
don''t like that idea though, but it is worth mentioning.


On 2008-01-04 01:26, pa**********@att.net wrote:

Below is posted from a link for Stanford students in computer
science.

QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or method.
QUOTE ENDS HERE

I have never heard anyone else say that it is a problem for a function
to return a reference. Are there really experts who object, or is
this nothing other than the commonplace observation that reference-
returning is a somewhat difficult concept that needs to be learned
carefully? (I am just learning about this now.) Assuming programmers
have some degree of competence and are able to avoid returning
references to locals and so on, what (if anything) are the pitfalls?

As far as I know there is only one pitfall, and that is returning a
reference to a variable local to the function, like so:

int& foo(int i)
{
int r = i+i;
return r;
}

int main()
{
int& twice = foo(5);
}

Since r is a local variable to foo() it will go out of scope as soon as
foo() is done executing, this means that the reference returned refers
to a variable that no longer exists.

On the other hand there are a number of valid reasons to return a
reference from a function, a function that returns an element from a
collection is a good example (look at the at() function in std::vector).

--
Erik Wikstr??m


On Jan 4, 12:26*am, pauldepst...@att.net wrote:

Below is posted from a link for Stanford students in computer
science.

QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or method.
QUOTE ENDS HERE

hmmm... I assume that following this extremely wise and useful advice,
Stanford students will not be allowed to do ...

#include <iostream>
int main(){
std::cout << "hello world\n";
}
regards
Andy Little

(Thankfully Not a C++ expert!) :-)


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

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