why won't this program work?

I would pls like to know why this program doesn't work, I've been trying to figure this one out for a while now and I just don't know why it doesn't work pls help! (first one doesn't work, second one works).



1.Doesn't work

include <iostream>
include <string>

int main()
{
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;
std::cout << message << std::endl;
return 0;
}







2.Works fine

include <iostream>
include <string>

int main()
{
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
std::cout << message << std::endl;
return 0;
}


Because "something" is a const char* and std::string("something"); is a std::string. strings have an overloaded '+' operator (that does what you expect it to), char*'s don't.

Topic archived. No new replies allowed.