2 ms·
That makes sense to me. Why does a pointer imply mutation? I thought that was what const is meant to signal.
by whimsicalism 6y ago
That makes sense to me. Why does a pointer imply mutation? I thought that was what const is meant to signal.
- ericbarrett 6y agoIt's convention in C++ to pass-by-pointer if the function will mutate the argument. The other way, by (non-const) reference, is discouraged because the function invocation looks the same as the extremely common pass-by-value case. void ptr_add(int *f) { *f += 5; } void val_add(int f) { f += 5; // only changes local copy of f } void ref_add(int &f) { f += 5; } int main() { int foo = 42; ptr_add(&foo); // foo is now 47 val_add(foo); // foo is still 47 ref_add(foo); // foo is now 52 }