[英]double vs. double* in C++
I'm developing a piece of code which makes scientific calculation, and as a result I use extensive use of double
variables.我正在开发一段代码来进行科学计算,因此我大量使用了
double
变量。 These variables travel from function to function, but most of them are never modified and hence defined as const
in function signatures.这些变量从一个函数传递到另一个函数,但它们中的大多数从未被修改过,因此在函数签名中被定义为
const
。
I have used double*
in function signatures, but then I started to wonder that whether it's better to use double
instead because:我在函数签名中使用了
double*
,但后来我开始怀疑使用double
是否更好,因为:
double*
and double
are same size, 8 bytes in C++. double*
和double
大小相同,在 C++ 中为 8 个字节。double*
, I need another fetch operation in the function.double*
时,我需要在函数中进行另一个 fetch 操作。 When using the variables in const
fashion, are there any valid reasons to use double*
instead of double
?当以
const
方式使用变量时,是否有任何正当理由使用double*
而不是double
?
Here's the crust of the matter:这是事情的实质:
const
, if you need to change the variable later, it's extra refactorization work, nevertheless const correctness is important .const
时要小心,如果您以后需要更改变量,这是额外的重构工作,但是const 正确性很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.