+ 1
Does "Hello" + '15' legal in C++...??
5 Answers
+ 8
This thread and its content is wrong on many different levels.
'15' in enclosed in single quotes, which tells the compiler to interpret it as a character constant, but contains more than a single character.
According to the standard:
"C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."
Putting the warning flag of 'implementation-defined' aside, the + operator for C++ is not overloaded to add char and strings. Referring to string operator+, you may see that + is overloaded:
string operator+ (const string& lhs, char rhs);
string operator+ (char lhs, const string& rhs);
But this works for a const string&, not const char* (string literals). Your string literals will have to be stored in string variables to properly utilize the overloaded operator.
http://www.cplusplus.com/reference/string/string/operator+/
We also have += overloaded for strings in a similar fashion.
http://www.cplusplus.com/reference/string/string/operator+=/
Even if the '15' here is meant as an integer, it wouldn't work either. Similarly, the + operator is not overloaded to cast or convert int values to string and to concatenate it with a string.
+ 5
No, in C++ you can only concatenate two strings like "stringa" + "stringb".
Something like "string" + 1 is not allowed(except in languages like Java
+ 5
@Yerucham and phpđ
+ 4
no, its different data types
+ 3
If Hello is a number and '15' is an integer, then yes. Otherwise, no.