I am just curious the same type cast format works for char, int and maybe many others, but why it does not work for string, i.e., what's wrong with (string) 'c'
behind the screen?
#include <iostream>
using namespace std;
int main(){
char a = 'a';
cout << (char) 99 <<endl;
cout << (int) 'c'<<endl;
cout<< (string) 'c' + "++" <<endl; // why this does not work???
cout<< string (1, 'c') + "++" <<endl;
return 0;
}
The problem is that there is no implicit conversion from an object of the type
char
to an object of the typestd::string
.You could write for example:
or
or (using the C casting)
or
As you can see from this line
there is used the constructor