c++ - Why pass a string by reference and make it a constant? -


just general question signature of function is:

void f( const string & s )... 

why necessary pass reference if not changing string (since constant)?

there 2 alternatives passing reference - passing pointer, , passing value.

passing pointer similar passing reference: same argument of "why pass pointer if not want modify it" made it.

passing value requires making copy. copying string more expensive, because dynamic memory needs allocated , de-allocated under cover. why better idea pass const reference / pointer passing copy not planning change.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -