返回指针或引用? [英] Return pointers or references?

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

问题描述




我正在设计一个共享库的界面,我想知道是否有一个关于如何标准的
将对象返回给用户。我
将使用异常来报告错误,因此至少有四种方法来实现它。


例如,如果我们有一个名为M的方法,它接收对

参数p的引用并返回对象o。


1 - 虚拟o& M(p&参数)抛出(Exception1,Exception2 ......

2 - 虚拟o * M(p&参数)抛出(Exception1,Exception2 ......


3 - 虚拟空虚M(p&参数,o& returnVal)抛出(异常1,

例外2 ...


4 - 虚拟空M(p&参数,o * returnVal)抛出(异常1,

异常2 ...

可能没有关于它的约定,只是一个味道,

但我想知道一些意见。


祝你好运,

Gama Franco

Hi,

I''m designing an interface for a shared library, and I would like to
know if there is a standard about how to return an object to the user. I
will use exceptions to report errors, so there are at least four ways to
do it.

For example if we have a method named M, that receives a reference to
parameter p and returns object o.

1 - virtual o& M(p& parameter) throw(Exception1, Exception2...

2 - virtual o* M(p& parameter) throw(Exception1, Exception2...

3 - virtual void M(p& parameter, o& returnVal) throw(Exception1,
Exception2...

4 - virtual void M(p& parameter, o* returnVal) throw(Exception1,
Exception2...
Probably there is no convention about it and is only a mater of taste,
but I would like to know some opinions.

Best regards,
Gama Franco

推荐答案

Gama Franco写道:
Gama Franco wrote:
我正在设计一个共享库的界面,我会喜欢知道是否有关于如何将对象返回给用户的标准。我将使用异常来报告错误,因此至少有四种方法可以做到这一点。

例如,如果我们有一个名为M的方法,它接收一个引用t o
参数p并返回对象o。

1 - 虚拟o& M(p&参数)抛出(Exception1,Exception2 ......
2 - 虚拟o * M(p&参数)抛出(Exception1,Exception2 ......

3 - 虚拟空隙M(p&参数,o& returnVal)抛出(异常1,
异常2 ......
4 - 虚拟空洞M(p&参数,o * returnVal)抛出(异常1) ,
Exception2 ...

可能没有关于它的约定,只是一个品味,但我想知道一些意见。
I''m designing an interface for a shared library, and I would like to
know if there is a standard about how to return an object to the user. I
will use exceptions to report errors, so there are at least four ways to
do it.

For example if we have a method named M, that receives a reference to
parameter p and returns object o.

1 - virtual o& M(p& parameter) throw(Exception1, Exception2...

2 - virtual o* M(p& parameter) throw(Exception1, Exception2...

3 - virtual void M(p& parameter, o& returnVal) throw(Exception1,
Exception2...

4 - virtual void M(p& parameter, o* returnVal) throw(Exception1,
Exception2...
Probably there is no convention about it and is only a mater of taste,
but I would like to know some opinions.




智能指针?


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces


Gama Franco写道:
Gama Franco wrote:
我正在设计一个共享库的界面,我想知道是否有关于如何使用的标准将对象返回给用户。我将使用异常来回购rt错误,所以至少有四种方法可以做到这一点。


更多,实际上。您仍然可以按价值返回。通常那是

首选方法。您可以通过引用传递指向对象的指针

并且您可以通过指针传递指向对象的指针。

例如,如果我们有一个名为M的方法,则接收对参数p的引用并返回对象o。


如果它返回对象o,为什么要尝试返回指针或

参考?不会返回对象意思是按价值?

1 - 虚拟o& M(p&参数)抛出(Exception1,Exception2 ......
2 - 虚拟o * M(p&参数)抛出(Exception1,Exception2 ......

3 - 虚拟空隙M(p&参数,o& returnVal)抛出(异常1,
异常2 ......
4 - 虚拟空洞M(p&参数,o * returnVal)抛出(异常1) ,
Exception2 ...

可能没有关于它的约定,只是一个品味,但我想知道一些意见。
I''m designing an interface for a shared library, and I would like to
know if there is a standard about how to return an object to the user. I
will use exceptions to report errors, so there are at least four ways to
do it.
More, actually. You still can return by value. And often that is
the preferred method. You can pass a pointer to an object by reference
and you can pass a pointer to an object by pointer.
For example if we have a method named M, that receives a reference to
parameter p and returns object o.
If it "returns object o", why are you trying to return a pointer or
a reference? Doesn''t "returns object" mean "by value"?

1 - virtual o& M(p& parameter) throw(Exception1, Exception2...

2 - virtual o* M(p& parameter) throw(Exception1, Exception2...

3 - virtual void M(p& parameter, o& returnVal) throw(Exception1,
Exception2...

4 - virtual void M(p& parameter, o* returnVal) throw(Exception1,
Exception2...
Probably there is no convention about it and is only a mater of taste,
but I would like to know some opinions.




差异很大。首先,你明确地返回

a指针或对_non_const_对象的引用。该对象在哪里

驻留?它是成员函数''M''

被调用的对象的一部分吗?然后返回引用或指针通常没问题,但是

返回对象的生命周期取决于你称之为M的对象的生命周期。所以,有一个依赖关系,

很多人宁愿避免。


其次,指针可以为null,所以任何时候有人都会向我返回指针

给我,我需要编写代码来验证它是否有效。无论你的一些大学伙伴说什么,参考都可以

永远不会为空。那么,

这里有一个重要区别。另一个区别是,如果它们是动态创建的对象,它更容易处理指针。

不,这不是不可能的在这种情况下的参考


int& ir = *(new int(42));

delete& ir;


但它很难看。所以,如果你在免费商店中创建一个对象,那么返回

一个指针。此外,彻底记录这一事实,以便用户不会忘记妥善处理对象(如果返回的价值为
值,则没有处理头痛)。


案例3和4假设(a)对象存在于外部并且(b)其类型

提供某种赋值语义。在这种情况下,你有一个问题的更多

将对象传递给函数的首选方式是什么

而不是什么是什么返回物品的首选方式。所以,与前两个不同,它的价格相当不同。


您可以将案例3和4合并为传递指向对象的指针通过

参考"这样你就可以在函数内部改变它指向

有意义的东西。在这种情况下,用户在调用函数之前不必创建对象
,并且可以返回动态分配的地址

。你不能引用另一个

引用,所以只有一个案例你没有提到:传递

a指针指针:


虚拟空虚M(p&参数,o ** returnValPtr)抛出(Exception1,..


这不错,但同样的含义适用:_you_需要检查

我没有向你传递空指针,当函数返回时,

我必须检查''returnValPtr'的值'之前我不是null

可以使用该对象。


Victor



The differences are significant. First of all, you clearly returning
a pointer or a reference to a _non_const_ object. Where is that object
residing? Is it part of the object for which the member function ''M''
is called? Then returning a reference or a pointer is often OK, but
the lifetime of the "returned" object will depend on the lifetime of
the object for which you call ''M''. So, there is a dependency, which
many prefer to avoid.

Second, a pointer can be null, so any time somebody returns a pointer
to me, I need to write code to verify that it''s valid. A reference can
never be null, no matter what some of your college buddies say. So,
here is one important difference. Another difference is that it''s much
easier to deal with pointers if they are to a dynamically created object.
No, it''s not impossible to work with a reference in that case

int &ir = *(new int(42));
delete &ir;

but it''s ugly. So, if you''re creating an object in free store, return
a pointer. Also, thoroughly document that fact so that the user won''t
forget to dispose of the object properly (in the case of returning by
value there is no disposal headache).

Cases 3 and 4 assume that (a) the object exists outside and (b) its type
provides some kind of assignment semantics. In that case, you have more
of a question "what''s the preferred way of passing objects into functions"
and not "what''s the preferred way of returning objects". So, it''s quite
different from the first two.

You could merge the cases 3 and 4 into "pass a pointer to an object by
reference" so that you could change it inside the function to point to
something meaningful. In that case the user doesn''t have to create
an object before calling your function and you can return the address of
something allocated dynamically. You cannot have a reference to another
reference, so there is only one more case you didn''t mention: pass
a pointer to a pointer:

virtual void M(p& parameter, o** returnValPtr) throw(Exception1, ..

which is not bad, but the same implication applies: _you_ need to check
that I didn''t pass a null pointer to you, and when the function returns,
I have to check that the value of ''returnValPtr'' is not null before I
can use the object.

Victor


Gama Franco写道:
Gama Franco wrote:

我正在为共享库设计一个界面,
我想知道
是否有关于如何返回对象的标准对于用户。我将使用异常来报告错误,
因此至少有四种方法可以做到这一点。

例如,如果我们有一个名为M的方法,
接收参考参数p并返回对象o。

1 - 虚拟o& M(p&参数)抛出(Exception1,Exception2 ......
2 - 虚拟o * M(p&参数)抛出(Exception1,Exception2 ......

3 - 虚拟空隙M(p&参数,o& returnVal)抛出(异常1,
异常2 ......
4 - 虚拟空洞M(p&参数,o * returnVal)抛出(异常1) ,
Exception2 ...

可能没有关于它的约定,只是品味问题,
但我想知道一些意见。

I''m designing an interface for a shared library,
and I would like to know
if there is a standard about how to return an object to the user. I
will use exceptions to report errors,
so there are at least four ways to do it.

For example if we have a method named M,
that receives a reference to parameter p and returns object o.

1 - virtual o& M(p& parameter) throw(Exception1, Exception2...

2 - virtual o* M(p& parameter) throw(Exception1, Exception2...

3 - virtual void M(p& parameter, o& returnVal) throw(Exception1,
Exception2...

4 - virtual void M(p& parameter, o* returnVal) throw(Exception1,
Exception2...
Probably there is no convention about it and is only a matter of taste,
but I would like to know some opinions.




以上都不是。

使用


virtual o M(const p& parameter)const;


代替。传递* const *引用并按值返回。



None of the above.
Use

virtual o M(const p& parameter) const;

instead. Pass by *const* reference and return by value.


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

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