我从来没有听说过其他人说这是一个函数 返回引用的问题。是否真的有专家反对,或者只是普通的观察参考 - 返回是一个需要学习的有点困难的概念 小心点? (我现在正在学习这个。)假设程序员 有一定的能力,并且能够避免返回 对当地人的引用等等,什么(如果有的话) )是否存在陷阱? 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 写道: 以下内容来自斯坦福大学学生的电脑链接 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 1月4日,12:26 * am,pauldepst ... @ att.net写道: 以下是来自斯坦福大学学生计算机的链接 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 computerscience.QUOTE BEGINS HEREBecause of the risk of misuse, some experts recommend never returningareference from a function or method.QUOTE ENDS HEREI have never heard anyone else say that it is a problem for a functionto return a reference. Are there really experts who object, or isthis nothing other than the commonplace observation that reference-returning is a somewhat difficult concept that needs to be learnedcarefully? (I am just learning about this now.) Assuming programmershave some degree of competence and are able to avoid returningreferences to locals and so on, what (if anything) are the pitfalls?Paul Epstein 解决方案 On Jan 3, 6:26 pm, [email protected] wrote:Below is posted from a link for Stanford students in computerscience.QUOTE BEGINS HEREBecause of the risk of misuse, some experts recommend never returningareference from a function or method.QUOTE ENDS HEREI have never heard anyone else say that it is a problem for a functionto return a reference. Are there really experts who object, or isthis nothing other than the commonplace observation that reference-returning is a somewhat difficult concept that needs to be learnedcarefully? (I am just learning about this now.) Assuming programmershave some degree of competence and are able to avoid returningreferences to locals and so on, what (if anything) are the pitfalls?Paul EpsteinI don''t see anything at all with returning references as long as theconst keyword is used correctly when appropriate. Surprisingly, a goodnumber of people do not use const when creating thier function stubs;const MyClass & GetMyClass() const; // The typical accessorSetMyClass(const MyClass & myclass); // The typical mutatorI constantly find the above two methods lacking the const keyword insource when I am debugging and it leads to misuse by other programmerswho simply glance at a method without taking into account the impactof making changes to the returned reference.Another common problem I run into is you can''t return NULL for areference. Sometimes it is nice to have the option to return NULL whenan error occured in which case I use a pointer and tell the caller tocheck the returned value to determine if an error occured. Some peopledon''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 computerscience.QUOTE BEGINS HEREBecause of the risk of misuse, some experts recommend never returningareference from a function or method.QUOTE ENDS HEREI have never heard anyone else say that it is a problem for a functionto return a reference. Are there really experts who object, or isthis nothing other than the commonplace observation that reference-returning is a somewhat difficult concept that needs to be learnedcarefully? (I am just learning about this now.) Assuming programmershave some degree of competence and are able to avoid returningreferences 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 areference 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 asfoo() is done executing, this means that the reference returned refersto a variable that no longer exists.On the other hand there are a number of valid reasons to return areference from a function, a function that returns an element from acollection is a good example (look at the at() function in std::vector).--Erik Wikstr??mOn Jan 4, 12:26*am, [email protected] wrote:Below is posted from a link for Stanford students in computerscience.QUOTE BEGINS HEREBecause of the risk of misuse, some experts recommend never returningareference from a function or method.QUOTE ENDS HEREhmmm... 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";}regardsAndy Little(Thankfully Not a C++ expert!) :-) 这篇关于返回参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 03:48