on-internet
← Community

Why are references to const necessary to avoid copying parameters

Stack Overflow ·

According to Wikipedia, function calls don't copy parameters which are are references to a const type: void f_slow(BigObject x); void f_fast(const BigObject& x); f_slow(y); // slow, copies y to parameter x f_fast(y); // fast, gives direct read-only access to y Why does the reference need to be const? Wouldn't a non-const reference accomplish the same: void f_should_be_fast(BigObject& x);

Comments

Sign in or sign up to comment and upvote.

No comments yet.