String as a parameter?

If I'm not going to change the line, then this is the first option better or both are the same - (I just want to iterate over the line, but since the line size is large, I do not want a local copy to be made.)

int getString(string& str1) {
    //code
}


int getString (string str1) {
    //code
}

And if I plan to change the line, then is there a difference between them? Is it because strings are immutable in C ++?

+5
source share
3 answers

String literals are immutable, std::stringare not.

The first is passing by reference. If you do not plan to change the line, pass it by reference const.

Secondly, transmission by value. - if you change the line inside the function, you would only modify the copy, so it's debatable

+7
source

const? , , .

int getString(string const &str1)
{
}
+9

, .

( &) getString. , , . (std::string(std::string& val)).

, ( &) , , const ( ). , , .

+2

All Articles