I'm studying about the C++ library type string. I was looking into operation that the type can perform, among which are concatenation. I know that in C++ operators can be overloaded to suit class types needs,and I know that character string literals are const char arrays under the hood. So, I know that the string type defines a constructor that takes a string literal argument and converts it into a string, so that I can initialize a string using:
string s="hello!"
what happens here is an impicit conversion (through the constructor that takes a const char*) to string and then a copy initialization is performed (through the copy-constructor of string).
so far so good.. (correct me if I am getting something wrong so far)
Now, I know that I can concatenate two strings, or I can concatenate a string and a char literal (or vice versa) and a string and a string literal (so I suppose the first happens because there's an overloaded + operator that takes symmetrically a char and a string, the latter happens thanks to the overloaded + opeartor that concatenates two strings and the implicit conversion from const char* to string). My questions are:
1: why can't I concatenate two string literals? shouldn't the operator+ that takes two strings be called (and two implicit conversion performed from const char* to string) ?
string s="hi"+" there";
2: I tried to concatenate a char literal and a string literal (calling the operator+ that takes a char and a string) but I get only garbage in the resulting string, as the compiler doesn't mind, but the result is certainly not what I want.
string s='h'+"ello!";
cout<<s<<endl;
I get garbage out.If a operator+(char,const string&) exists it should be called after implicitly converting "ello" to string shouldn't it?
can you please explain?