Based on my reading, the following code:
string aggregate = "give" + 'n';
Should produce a resulting string with the value:
"given".
It instead produces garbage. Why doesn't the following happen?
"give" is converted to a std::string via the constructor that takes a pointer to a character array.
The '+' overload that takes a std::string and a character is called, returning a new string.
I am basing my theory on this man page.
Now, I have heard that the first argument to an overloaded operator isn't a candidate for constructor conversion if the operator is a member of a class. I believe I read that in Koenig and Moo. But, in this case I understand the '+' operator to be a non-member overload.
I realize this seems like a ridiculous over-complication, but I like to know FOR SURE what is happening when I write code.
The expression "give" + 'n'
is evaluated first, adding (int)'n'
to the address of the string constant "give"
. The result of this pointer arithmatic is assigned to the string object. No error is raised because the result is of type const char*
.
You should get used to thinking of everything on the right of the =
as happening before the actual assignment (or initialization in this case).
You want the following:
// construct the string object first
std::string aggregate = "give";
// then append the character
aggregate += 'n';
or, explicitly build the a string object first:
std::string aggregate = std::string("give") + 'n';
The fact that there is a std::string
on the left side of the expression doesn't matter for the first step: "give"
is a const char[5]
which decays to a const char*
and 'n'
is a char
, that is, an integer type. Adding an integer to a pointer simply adds to the pointer's address. The result is again of type const char*
, which is then implicitly converted to std::string
.
Basically, your code is equivalent to:
const char* c = "a string" + 'n';
std::string s = c;
So, to achieve the desired effect, try
std::string s = std::string("a string") + 'n';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With