参考参数 [英] Reference parameters

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

问题描述

我被其他人鼓励使用引用而不是

指针作为out函数的参数,例如,


void doStuff(Thing& out1,Thing& out2)


而不是:


void doStuff(事情* out1,事情* out2)


我遇到的问题是,当调用该函数时,你不能

告诉我们正在修改这个值:


doStuff(thing1,thing2);


这个要少得多显而易见:


doStuff(& thing1,& thing2);


我在这里使用指针可以想到的唯一缺点是:$ / b

- 错误地传入NULL指针

- doStuff()的主体稍微冗长(* out1而不是out1, - >

而不是。")

- 我想指针可能会被意外重新分配,增加,

删除等等


我会将功能的可读性和清晰度排在其中,因为这些功能比任何这些东西都要重要(因为mos)一个程序的生命周期是

通常维护,不一定是原作者)。

的朋友鼓励使用引用说它是更多C ++ ish,但

本身并不是一个非常好的论据。 ..


有任何意见吗?


David F


PS。我知道
http: //www.parashift.com/c++-faq-lit...s.html#faq-8.6 ...

I have been encouraged by someone else to use a reference rather than a
pointer as an "out" parameter to a function, eg.

void doStuff(Thing &out1, Thing &out2)

as opposed to:

void doStuff(Thing *out1, Thing *out2)

The issue I have with this is that when the function is called, you cannot
tell that the value is being modified:

doStuff(thing1, thing2);

This is much less obvious than:

doStuff(&thing1, &thing2);

The only disadvantages I can think of with using pointers here are:

- a NULL pointer could be passed in by mistake
- the body of doStuff() is slightly more verbose (*out1 instead of out1, ->
instead of ".")
- I suppose the pointer could be accidentally reassigned, incremented,
deleted, etc

I would rank readability and clarity in the use of functions as more
important than any of these things (since most of a program''s lifetime is
usually maintenance, not necessarily by the original writer). The friend who
was encouraging the use of references said that it was "more C++ish", but
that by itself isn''t a very good argument ...

Any comments ?

David F

PS. I am aware of
http://www.parashift.com/c++-faq-lit...s.html#faq-8.6 ...

推荐答案

On Sun,2003年11月30日17:54:29 +1100,David Fisher

< no **** @ nospam.nospam.nospam>写道:
On Sun, 30 Nov 2003 17:54:29 +1100, "David Fisher"
<no****@nospam.nospam.nospam> wrote:
其他人鼓励我使用引用而不是
指针作为out函数的参数,例如,

void doStuff(Thing& out1,Thing& out2)

而不是:

void doStuff (事情* out1,事情* out2)

我遇到的问题是,当调用该函数时,你不能告诉该值被修改:

doStuff(thing1,thing2);

这远不如:

doStuff(& thing1,& thing2);

- 可能会错误地传入NULL指针
- doStuff()的主体稍微冗长(* out1而不是out1, - >
而不是。")
- 我认为指针可能会被意外重新分配,增加,删除等等

>我将使用函数的可读性和清晰度排在比任何这些东西更重要(因为大多数程序的生命周期通常是维护,不一定是由原始维护作家)。
鼓励使用引用的朋友说它是更多C ++ ish,但
本身并不是一个非常好的论据......

任何评论?
I have been encouraged by someone else to use a reference rather than a
pointer as an "out" parameter to a function, eg.

void doStuff(Thing &out1, Thing &out2)

as opposed to:

void doStuff(Thing *out1, Thing *out2)

The issue I have with this is that when the function is called, you cannot
tell that the value is being modified:

doStuff(thing1, thing2);

This is much less obvious than:

doStuff(&thing1, &thing2);

The only disadvantages I can think of with using pointers here are:

- a NULL pointer could be passed in by mistake
- the body of doStuff() is slightly more verbose (*out1 instead of out1, ->
instead of ".")
- I suppose the pointer could be accidentally reassigned, incremented,
deleted, etc

I would rank readability and clarity in the use of functions as more
important than any of these things (since most of a program''s lifetime is
usually maintenance, not necessarily by the original writer). The friend who
was encouraging the use of references said that it was "more C++ish", but
that by itself isn''t a very good argument ...

Any comments ?




就个人而言,我更喜欢使用指针来输出参数。对于一切

否则我使用const引用。如果传递给函数的东西可以被修改,那么能够立即告诉

的优势超过了我所看到的缺点。


我不买指针参数本质上不安全的论点。

如果调用者总是使用现有变量的地址,那么

将是没有空指针担心。另一方面,如果来电者

只有指针可供使用,那么如果你拿一个

参考,你也不会更好。解除引用的NULL指针和NULL指针一样糟糕。

断言指针参数是非NULL是很自然的,并且是任何开发人员在做的第一件事就是

一个带指针的函数。如果你接受了一个引用,你就可以很容易地传递NULL引用,

但是大多数开发人员通常都不会检查它。

当然,调用者有责任检查传递给函数的

参数的有效性,但这两种情况都是如此。


-

见到你。



Personally, I prefer to use pointers for out parameters. For everything
else I use const references. The advantage of being able to tell
instantly if something passed to a function could be modified outweighs
the disadvantages I''ve seen.

I don''t buy the argument that pointer arguments are inherently unsafe.
If the caller is always taking the address of existing variables, there
will be no NULL pointers to worry about. On the other hand if the caller
only has pointers at its disposal, you''re no better off if you take a
reference. A dereferenced NULL pointer is as bad as a NULL pointer.
Asserting that a pointer argument is non-NULL is natural, and is the
first thing any developer would do in a function that takes pointers. If
you take a reference, you can be passed NULL-references just as easily,
but most developers typically would not check for that.

Sure, it''s the caller''s responsibility to check the validity of the
arguments passed to the function, but that''s true for both cases.

--
Be seeing you.


DavidFisherescribió:
David Fisher escribió:
其他人鼓励我使用引用而不是
指针作为out。函数的参数,例如,

void doStuff(Thing& out1,Thing& out2)

而不是:

void doStuff (事情* out1,事情* out2)

我遇到的问题是,当调用该函数时,你不能告诉该值被修改:

doStuff(thing1,thing2);

这远不如:

doStuff(& thing1,& thing2);
I have been encouraged by someone else to use a reference rather than a
pointer as an "out" parameter to a function, eg.

void doStuff(Thing &out1, Thing &out2)

as opposed to:

void doStuff(Thing *out1, Thing *out2)

The issue I have with this is that when the function is called, you cannot
tell that the value is being modified:

doStuff(thing1, thing2);

This is much less obvious than:

doStuff(&thing1, &thing2);




然后重命名函数:


evaluateThings(thing1,thing2);


问候。



Then rename the function:

evaluateThings (thing1, thing2);

Regards.


On Sun,2003年11月30日01:25:35 -0600,Thore Karlsen< si*@6581.com>写道:
On Sun, 30 Nov 2003 01:25:35 -0600, Thore Karlsen <si*@6581.com> wrote:
就个人而言,我更喜欢使用指针输出参数。对于一切
否则我使用const引用。


查看Andrei Alexandrescu的现代C ++设计自动选择

介于按值之间和by const ref。


如果能够修改传递给某个函数的东西,能够立即告诉
我的优点是什么? '见过。


不幸的是,优势要求(1)没有参数包含

引用或指针,这将优势限制在一个非常小的子集中

函数,(2)熟悉它所属的特定函数或函数集
它所属的b $ b,大概它不是真正的优势,对于

学位是有利的,(3)对于熟悉的一套

函数,他们都严格遵守非参考公约,而不是

单个未知的传递参考案例,(4)一个或多个函数

设计得非常糟糕,以至于对他们的

效果有任何疑问(即他们可能不是你的功能),以及(5)你可以从调用代码中看到
总是看到参数是否是指针。


在所有这些限制下,我认为这是一个非常可疑的优势。


没有必要调用这样的优势来证明使用指针。

我不买指针参数本身就不安全的论点。
如果调用者总是使用现有变量的地址,那么
将无需担心的NULL指针。另一方面,如果调用者只有指针可供使用,那么如果你参考了
,你就不会有更好的选择。取消引用的NULL指针与NULL指针一样坏。
断言指针参数是非NULL是很自然的,并且是任何开发人员在带指针的函数中做的第一件事。如果您接受引用,您可以像传容一样轻松地传递NULL引用,但大多数开发人员通常不会检查它。


因为在调用者和被调用者中检查NULL指针是多余的;

此外,因为检查引用参数的地址

非NULL取决于正式的未定义效果,所以最多是假的

安全性(这是危险的);最重要的是,因为参考论证

是来电者的合同义务;见下文。


当然,调用者有责任检查传递给函数的
参数的有效性,但这是真的。两种情况。
Personally, I prefer to use pointers for out parameters. For everything
else I use const references.
Check out Andrei Alexandrescu''s "Modern C++ Design" for automatic choice
between "by value" and "by const ref".

The advantage of being able to tell
instantly if something passed to a function could be modified outweighs
the disadvantages I''ve seen.
Unfortunately that advantage requires (1) that no argument contains a
reference or pointer, which restricts the advantage to a very small subset
of functions, (2) familiarity with the specific function or set of functions
that it belongs to, where presumably it''s less of a real advantage, to the
degree that is any advantage, (3) for the case of a familiar set of
functions, that they all adhere strictly to the non-ref convention, not a
single unknown case of pass-by-reference, (4) that the function or functions
are sufficiently badly designed that there can be any doubt about their
effects (i.e. they''re presumably not your functions), and (5) that you can
always see from the calling code whether the arguments are pointers or not.

With all these restrictions it seems to me to be a very dubious advantage.

There''s no need to invoke such an advantage to justify using pointers.
I don''t buy the argument that pointer arguments are inherently unsafe.
If the caller is always taking the address of existing variables, there
will be no NULL pointers to worry about. On the other hand if the caller
only has pointers at its disposal, you''re no better off if you take a
reference. A dereferenced NULL pointer is as bad as a NULL pointer.
Asserting that a pointer argument is non-NULL is natural, and is the
first thing any developer would do in a function that takes pointers. If
you take a reference, you can be passed NULL-references just as easily,
but most developers typically would not check for that.
Because checking for NULL-pointers in both caller and callee is redundant;
furthermore, because checking that the address of a reference argument
is non-NULL depends on a formally undefined effect and so is at best false
security (which is dangerous); and most of all, because a reference argument
is a contractual obligation on the caller; see below.

Sure, it''s the caller''s responsibility to check the validity of the
arguments passed to the function, but that''s true for both cases.




查看按合同设计(DBC)的理念,例如正如在面向对象的软件构建中所描述的那样,Betrand Meyer本人所描述的。这个理念的关键是要明确地将要求和责任放在调用者(客户代码)和被调用者(例行程序)上,以便避免


冗余并提高清晰度。一些C ++程序员使用引用参数

来表示参数不能为NULL(源于语言

定义,因此对调用者是绝对要求),以及指针

参数表示那些参数可能是NULL(这只是一个模糊的
和依赖于上下文的约定,必须检查它是否不清楚)。



Check out the Design By Contract (DBC) philosophy, e.g. as described by
Betrand Meyer himself in "Object Oriented Software Construction". The crux
of that philosophy is to clearly place requirements and responsibilities on
the caller (client code) and the callee (routine) so as to both avoid
redundancy and increase clarity. Some C++ programmers use reference arguments
to express that an argument cannot be NULL (which stems from the language
definition, and so is an absolute requirement on the caller), and pointer
arguments to express that those arguments may be NULL (which is just a vague
and context-dependent convention, must be checked if it''s not clear).


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

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