std::string as function parameter
Hello,
I have planty of functions which have std::string as parameter
the string value isn't change inside those functions (I send them as a const)
My question is, should I pass the std::string by value like it was int/double etc
or by reference like I do with structs?
I mean
const std::string myString
func1(myString)
should I define func1 like this:
std::string func1(const std::string& myStr)
or
std::string func1(const std::string myStr)
is std::string is a big structure needs using reference?
thanks a lot
avi
Re: std::string as function parameter
Quote:
is std::string is a big structure needs using reference?
Well...as dude_1967 already indicated...if a function does not change the contents of a variable you should basically pass them as a constant reference. However, this is only a matter while dealing with structures and/or classes. Simple data types can be still passed by value since passing by reference would only add unnecessary overhead.
In addition, you might want to take a look at this article....