问题描述
具体来说,我们之间有什么区别
void MyFunction(const HANDLE myhandle
an
void MyFunction(const HANDLE& myhandleref
??
后者可能会导致问题(例如我使用HANDLE指针时遇到的问题)以这种方式
任何函数在使用时都会引起问题参考和
指针也不例外。如果你通过引用传递一个句柄,你就是让b / b
让被调用函数改变句柄值如果你通过
const reference或const传递它,你就会抑制这些变化。
HANDLEs是值类型 - 它们通常应该通过按值,不是
const,不是引用,不是指针。像对待int一样对待它们。通过引用传递
只会减慢代码速度,因为你正在添加一个
间接级别。通过const值传递它们是没有意义的,因为
内的变化函数不能逃避。无论如何。
void MyFunction(HANDLE mh)
-cd
的确如此。因为它们是副本。
Arnold the Aardvark
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
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
Indeed. Because they are copies.
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp
这篇关于处理/参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!