When you do
string s = 'c';
you are basically invoking constructor initialisation rather than an assignment operation. But there isn’t any constructor for std::string
that takes only a single char
as input. There is however one std::string(n, c)
, where n
is the number of characters c
in the string.
When you do
s = 'c'
you do an assignment operation, invoking the overloaded string::operator=
(string& operator= (char c);
) for std::string
. Now this method is overloaded to accept a single char
as input as well, as you can see from the code snippet at this reference as well as at this one.
std::string str1;
// ...
// (4) operator=( CharT );
str1 = '!';
Additionally, std::string::assign
doesn’t accept a single char
, similar to the constructor.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…