处理/参考? [英] handle/reference?

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

问题描述

具体来说,我们之间有什么区别


void MyFunction(const HANDLE myhandle


an


void MyFunction(const HANDLE& myhandleref


??

后者可能会导致问题(例如我使用HANDLE指针时遇到的问题)以这种方式

解决方案

Bonj写道:

具体来说,使用

void MyFunction(const HANDLE myhandle)



void MyFunction(const HANDLE& myhandleref)

???后者可能会导致问题(比如我经历过用这种方式使用的HANDLE指针)




任何函数在使用时都会引起问题参考和

指针也不例外。如果你通过引用传递一个句柄,你就是让b / b
让被调用函数改变句柄值如果你通过

const reference或const传递它,你就会抑制这些变化。


HANDLEs是值类型 - 它们通常应该通过按值,不是
const,不是引用,不是指针。像对待int一样对待它们。通过引用传递

只会减慢代码速度,因为你正在添加一个

间接级别。通过const值传递它们是没有意义的,因为

内的变化函数不能逃避。无论如何。


void MyFunction(HANDLE mh)


-cd


对,非常感谢muc

这是'间接水平''我在设计代码结构时没有考虑到的事情,但是它已经清除了它

> HANDLEs是值类型 - 它们通常应该通过值传递,而不是

const,而不是引用,而不是指针。像对待int一样对待它们。
通过引用传递它们只会减慢代码速度,因为你正在添加一个
级别的间接。


澄清你说的话:


这取决于复制对象的成本是多少

你正在路过,因为你实际上正在传递副本。 HANDLE可能很小 - 一个int或类似的 - 我不记得了。

一般来说,将const引用传递给更大的对象更多

有效。

通过const值传递它们是没有意义的,因为函数内的变化不能逃避。无论如何。




的确如此。因为它们是副本。

Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp


Specifically, is there any difference between usin

void MyFunction(const HANDLE myhandle

an

void MyFunction(const HANDLE& myhandleref

??
is the latter likely to cause problems (such as I have experienced with HANDLE pointers used in this way

解决方案

Bonj wrote:

Specifically, is there any difference between using

void MyFunction(const HANDLE myhandle)

and

void MyFunction(const HANDLE& myhandleref)

???
is the latter likely to cause problems (such as I have experienced
with HANDLE pointers used in this way)



Any function will cause problems when used incorrectly. Reference and
pointers are no exception. If you pass a handle by reference, you''re
letting the called function change the handle value. If you pass it by
const reference or const, you''re inhibiting such changes.

HANDLEs are value types - they should generally be passed by value, not
const, not reference, not pointer. Treat them as you would an int. Passing
them by reference just slows the code down, since you''re adding a level of
indirection. Passing them by const value is pointless since changes within
the function can''t "escape" anyway.

void MyFunction(HANDLE mh)

-cd


Right, thanks very muc
It''s the ''level of indirection'' thing I''m failing to consider when designing the code structure, but that''s cleared it up


> HANDLEs are value types - they should generally be passed by value, not

const, not reference, not pointer. Treat them as you would an int.
Passing them by reference just slows the code down, since you''re adding a
level of indirection.
To clarify what you''ve said:

It rather depends on how expensive it is to copy the object
you are passing, since you are in fact passing a copy. HANDLE is
likely to be pretty small - an int or similar - I can''t remember.
In general, passing a const reference to a larger object is more
efficient.
Passing them by const value is pointless since changes
within the function can''t "escape" anyway.



Indeed. Because they are copies.
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp


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

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