After some years without touching c++, I am getting back at it and decided to study again using a text book ("Sutherland, Bruce. Learn C++ for Game Development (p. 59). Apress. Kindle Edition."). I have 2 quick questions:
1) In one example, the book uses the following lines of code to call a function:
string playerInput;
GetPlayerInput( playerInput);
And the function itself is defined as:
void GetPlayerInput(string& playerInput)
{
cin >> playerInput;
}
Question: The "playerInput" being passed as "string&". Is it really necessary? Why is this being passed as reference when we could just make the function return a string and do it like this:
string playerInput;
playerInput = GetPlayerInput();
And:
string GetPlayerInput()
{
cin >> playerInput;
return playerInput;
}
Is this just a stylistic choice or I am missing something here?
2) An class accessor method to get a user name was defined as:
'void SetName(const std::string& name);'
No other explanation. What's up with this "const" thing? I did some research but only got more confused. Why would you need it when passing a string as a parameter (and again as reference??)
Thank you so much for any help!