How to pass “literal” integers by reference in C++ (Newbie)

后端 未结 5 454
温柔的废话
温柔的废话 2020-12-03 17:23

Edit: As many people have pointed out, pass-by-reference isn\'t generally appropriate as an optimisation for primitive types. This is excellent to know, so

5条回答
  •  有刺的猬
    2020-12-03 17:50

    To avoid the inefficiency of copy-by-value when calling a function

    Stop right there.

    Passing by reference does not necessarily mean "fast". This goes doubly so for basic types. Indeed, accessing basic types by reference will be slower than doing so by value. A reference is not magic. It's generally implemented as a pointer. So every time you access that value, you are likely doing a pointer dereference.

    This sounds like some kind of micro-optimization. Do not optimize without profiling. Unless you have really good reason to expect the performance of your application to hinge on value vs. reference parameters, just do what makes sense.

    You should only pass basic types by reference if you intend to modify them.

提交回复
热议问题