With
s="b"+"k"+"4";
your literal strings will decay to pointers, and you will add those pointers together, and assign the result to s
. That pointer addition will make no sense, there's no overloaded +
operator function which takes two const char *
arguments.
But with
s=s1+"b"+"k"+"4";
you start with a std::string
object, for which there are overloaded +
operator functions, which return a std::string
object. It is in short something similar to
s=((s1+"b")+"k")+"4";
That is you have s1 + "b"
, which returns a temporary std::string
which you then add "k"
to, which returns a temporary std::string
to which you add "4"
, which returns a temporary std::string
which is assigned to s
. Or with the temporary objects and functions calls explicitly used:
std::string t1 = operator+(s1, "b");
std::string t2 = operator+(t1, "k");
std::string t3 = operator+(t2, "4");
s = t3;