简体   繁体   English

C++ 中的 double 与 double*

[英]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 个字节。
  • When I send in 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:这是事情的实质:

  • First of all it's always a good idea to use pass-by-value for native data types (see here ) if you have no special requirements such as:首先,如果您没有特殊要求,则对本机数据类型使用按值传递始终是一个好主意(请参阅此处),例如:
    • Transferring arrays via the variable通过变量传输数组
    • Keep inter-object relations (as outlined here )保持对象间的关系(如此所述)
    • And similar和类似的
  • Transferring references have negligible performance affects for most cases.在大多数情况下,传输参考对性能的影响可以忽略不计。
  • Be careful while using 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM