i'm new to c++ and have a lack of understanding why this code works well:
string GetString(string promt)
{
cout << promt << ": ";
string temp;
getline(cin, temp);
return temp;
}
int main()
{
string firstName = GetString("Enter your first name");
string lastName = GetString("Enter your last name");
cout<< "Your Name is: " << firstName << " " << lastName;
cin.ignore();
cin.get();
return 0;
}
String-Literals like "bla" are of Type const char*. At least auto i = "bla"; indicates, that i is of Type "const char*". Why is it possible to pass it to the GetString-Function, because the function expects a string and not a const char*?
const char(&)[N]
, but since C can't copy an array to the local variable,auto
makes the localconst char*
anyway. – Lieselotteliestal