I've used cpp for quite a while, I was known that we cannot add string and numbers(as + operator is not overloaded for that). But , I saw a code like this.
#include <iostream>
using namespace std;
int main() {
string a = "";
a += 97;
cout << a;
}
this outputs 'a' and I also tried this.
string a ="";
a=a+97;
The second code gives a compilation error(as invalid args to + operator, std::string
and int
).
I don't want to concatenate the string and number.
What is the difference? Why does one work but not the other?
I was expecting that a+=97
is the same as a=a+97
but it appears to be different.