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
Post a Comment