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

后端 未结 5 463
温柔的废话
温柔的废话 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:33

    First you should take heed of the other's advice to prefer passing by value for simple types. C (and by extension C++) were designed with this use case in mind, and that's what they're optimized for.

    Second you should always use a const reference unless you intend to modify the variable. A literal can be bound to a const reference but not a non-const one.

提交回复
热议问题