Complete C++ beginner here. Can someone help me understand the difference between these two approaches for passing a string into a function in C++? The second version I saw in an example online.
On the surface, both seem to work fine - what is the purpose of using the additional "const" and the "&" in this case? Does it have some implications for memory usage inside the function itself only (because the "&" means it is a reference) ?
#include <iostream>
using namespace std;
void printPhrase(string phrase_to_print){}
cout << phrase_to_print << endl;
}
int main(){}
std::string phrase = "This is my phrase";
printPhrase(phrase);
return 0;
}
#include <iostream>
using namespace std;
void printPhrase(const std::string& phrase_to_print){
cout << phrase_to_print << endl;
}
int main(){
std::string phrase = "This is my phrase";
printPhrase(phrase);
return 0;
}