选择函数参数类型 - 指针或对象? [英] Choosing function parameter types - pointers or objects?
问题描述
目前,我只编写接受指针作为
参数的函数来改变一个值,但我已经意识到它经常是
是一个通过指针而不是实际的
对象更有效率。
我知道这个问题有点模糊,但我想知道任何
规则/指南/样式问题,何时使用指针,何时
不要,在你的函数参数中?
我也是对任何灰色区域感兴趣,甚至是像win32 API中那样编写了关于
的非主题函数,但我怀疑
其他人是谁。
干杯,马特
ballpointpenthief说:
我知道这个问题有点模糊,但是我想知道任何
规则/指南/样式问题,什么时候使用指针,什么时候
不在你的函数参数中?
如果它是原生类型(int,short,long,double,那种东西),除非你以原样传递
需要破解它的价值。还有别的东西 - 结构和
东西 - 传递指针。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
ballpointpenthief张贴:
我知道这个问题有点模糊,但我想知道任何
规则/指南/风格问题,什么时候使用指针,什么时候
不要,在你的函数参数?
你将得到的效率:
(1)按值传递对象(因此在过程中复制它。
和
(2)传递其地址,并取消引用地址。
对于内在类型,(1)更快。
对于一个巨大的类型,其对象可能是千字节,(2)将是
更快。
我相信其他人可以给你更详细的回复。
-
弗雷德erick Gotham
Richard Heathfield< in ***** @ invalid.invalid>写道:ballpointpenthief说:我知道这个问题有点模糊,但我想知道任何
规则/指南/风格问题,对于什么时候来说在你的函数参数中使用指针,什么时候不用?
如果它是本机类型(int,short,long,double,那种东西) ,除非你需要破解它的价值,否则将其传递给原始版本。还有其他东西 - 结构和
东西 - 传递一个指针。
我不会毫不犹豫地直接传递* small *结构。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
At the moment, I only write functions which accept pointers as
arguments in order to change a value, but I''ve realised it would often
be a lot more effiecient to pass a pointer rather than an actual
object.
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?
I''m also interested in any grey areas, or even off-topic functions that
have been written like in the win32 API for instance, but I doubt
anyone else is.
Cheers, Matt
解决方案ballpointpenthief said:
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?
If it''s a native type (int, short, long, double, that sort of thing), pass
it in raw unless you need to hack its value. Anything else - structs and
stuff - pass a pointer.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
ballpointpenthief posted:
I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?
You''d have the measure the efficiency of:
(1) Passing the object by value (thus copying it in the process).
and
(2) Passing its address, and dereferencing the address.
For the intrinsic types, (1) is faster.
For a huge type, an object of which is perhaps a kilobyte, (2) would be
faster.
I''m sure the others can give you a more detailed reply.
--
Frederick Gotham
Richard Heathfield <in*****@invalid.invalid> writes:ballpointpenthief said:I know this question is a bit vague, but I was wondering about any
rules / guidelines / style issues, for when to use pointers, and when
not to, in your function parameters?
If it''s a native type (int, short, long, double, that sort of thing), pass
it in raw unless you need to hack its value. Anything else - structs and
stuff - pass a pointer.
I wouldn''t hesitate to pass a *small* structure directly.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
这篇关于选择函数参数类型 - 指针或对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!