bayindirh
bayindirh

Reputation: 425

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. These variables travel from function to function, but most of them are never modified and hence defined as const in function signatures.

I have used double* in function signatures, but then I started to wonder that whether it's better to use double instead because:

When using the variables in const fashion, are there any valid reasons to use double* instead of double?

Upvotes: 1

Views: 11426

Answers (1)

bayindirh
bayindirh

Reputation: 425

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.

Upvotes: 1

Related Questions