Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
non-const-references [2015/01/07 21:03] – created awf | non-const-references [2016/06/23 08:51] (current) – awf | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Avoid non-const references ==== | + | ===== Avoid non-const references |
- | In general, arguments should be values, const references, or pointers, | + | [I know this is in the [[https:// |
+ | |||
+ | In general, arguments should be values, const references, or pointers, | ||
Well, you might write a function '' | Well, you might write a function '' | ||
Line 42: | Line 44: | ||
</ | </ | ||
+ | ==== But... ==== | ||
+ | If your function is more complex than inc, and you believe the code is more readable with the reference, just capture the argument locally. | ||
+ | < | ||
+ | void set_first_element_to_one_if_has_at_least_one_elemet(std:: | ||
+ | { | ||
+ | if (pv-> | ||
+ | (*pv)[0] = 1; // Don't like this? | ||
+ | auto& v = *pv; | ||
+ | v[0] = 1; // Look, much nicer | ||
+ | } | ||
+ | </ |