输出参数并通过引用传递 [英] Out parameters and pass by reference

查看:126
本文介绍了输出参数并通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加入了一个新的群组,该群组的编码指南(对我而言)似乎过时了。



但是只是反对机器没有有效的备份是不会得到我在任何地方。

所以我转向SO,看看我们是否可以理性的理由/反对(他可能是错误的我的选择,所以双方的意见将不胜感激。)。 p>

参数的准则是:


提示:

  void Func1(CFoo& Return); // bad 
void Func2(CFoo * pReturn); // good

说明:

使用引用,它看起来与值相同。调用者可能会惊讶他的值在调用函数后已更改。被叫者可能无意地修改该值而没有意义影响呼叫者的值。通过使用指针,调用者和被调用者都清楚可以改变该值。在代码审查中使用引用可能会造成特别误导。



解决方案


当您使用引用时,它看起来与值相同。


只有当您真的没有注意到您做。好的,有时会发生,但真的...没有数量的编码标准可以纠正人们不注意或不知道他们在做什么。



调用者可能会惊讶于他的值在调用函数后发生了变化。你调用一个函数,那么该函数记录不良。



给定一个函数的名称,它的参数列表,以及一些非常简短和描述性的文档,它应该清楚地说明函数的作用及其可观察的副作用是什么包括是否修改任何参数)。


被调用者可以无意义地修改值,而不影响调用者的值。


如果函数是const正确的,那么这不是问题。如果函数不是const正确的,那么应该使const正确,如果你可以(追溯地使代码const正确可以绝对跳动)。



没有什么意义,虽然:当你实际上写一个函数的代码,你应该能够看到参数的声明。如果函数太长,你不能,是时候重构了。


通过使用指针,调用者和被调用者可以更改该值。


这不完全正确。


使用引用在代码审查中可能特别容易误导


仅当执行代码审查的人不知道他们在做什么。






所有这些都很好,但为什么要使用pass-by-reference而不是pass-by-pointer?最明显的原因是引用不能为null



在一个接受指针的函数中,必须检查指针在使用它之前不是null,至少是使用调试断言。在正确的代码审查期间,您必须分析更多的代码,以确保您不会意外地传递一个空指针到一个不期望的函数。我发现,需要更长时间来审查带有指针参数的函数,因为这个原因;当使用指针时,它变得容易出错。


I have joined a new group that has coding guidelines that (to me) seem dated.

But just rallying against the machine without valid backup is not going to get me anywhere.
So I am turning to SO to see if we can up with rational reasons for/against (hey I may be wrong in my option so both sides of the argument would be appreciated).

The guideline that is up for argument is:

Tip: Use pointers instead of references for return arguments.

void Func1( CFoo &Return );  // bad  
void Func2( CFoo *pReturn ); // good  

Justification:
When you use a reference, it looks the same as a value. The caller may be surprised that his value has been changed after calling the function. The callee may innocently modify the value without meaning to affect the caller's value. By using a pointer, it is clear to both the caller and callee that the value can be changed. Using references can be particularly misleading in code reviews.

解决方案

When you use a reference, it looks the same as a value.

Only if you really aren't paying attention to what you are doing. Ok, sometimes that happens, but really... no amount of coding standards can correct for people not paying attention or not knowing what they are doing.

The caller may be surprised that his value has been changed after calling the function.

If you are surprised by what happens when you call a function, then the function is poorly documented.

Given a function's name, its parameter list, and perhaps some very brief and descriptive documentation, it should be eminently clear what the function does and what its observable side effects are (including whether any arguments are modified).

The callee may innocently modify the value without meaning to affect the caller's value.

If the function is const correct, then this isn't a problem. If the function isn't const correct, then it should be made const correct, if you can (retroactively making code const correct can be an absolute beating).

This rationale doesn't make much sense, though: when you are actually writing the code for a function, you should be able to see the declarations of the parameters. If the function is so long that you can't, it's time for refactoring.

By using a pointer, it is clear to both the caller and callee that the value can be changed.

This is not entirely correct. A function can take a pointer to const object, in which case the object cannot be changed.

Using references can be particularly misleading in code reviews.

Only if the people doing the code reviews don't know what they are doing.


All of that is well and good, but why should pass-by-reference be used instead of pass-by-pointer? The most obvious reason is that a reference cannot be null.

In a function that takes a pointer, you have to check that the pointer is not null before you use it, at least with a debug assertion. During a proper code review you have to analyze more code to be sure that you don't accidentally pass a null pointer to a function that doesn't expect one. I've found that it takes much longer to review functions that take pointer arguments for this very reason; it's so much easier to get it wrong when using pointers.

这篇关于输出参数并通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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