+ 1

Does "Hello" + '15' legal in C++...??

24th Dec 2017, 7:54 AM
Nemish Garg
Nemish Garg - avatar
5 Respostas
+ 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.
24th Dec 2017, 10:14 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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
24th Dec 2017, 9:30 AM
David Akhihiero
David Akhihiero - avatar
+ 5
@Yerucham and php😊
24th Dec 2017, 9:31 AM
Botol
Botol - avatar
+ 4
no, its different data types
24th Dec 2017, 8:47 AM
Botol
Botol - avatar
+ 3
If Hello is a number and '15' is an integer, then yes. Otherwise, no.
24th Dec 2017, 8:57 AM
blackcat1111
blackcat1111 - avatar