+ 9

Operators

can operators be overloaded to allow string and numeric concatenation?

7th Aug 2017, 12:07 AM
Karl
Karl - avatar
14 Answers
+ 12
Yes. I think this is possible. Just explicitly convert the numeric value to string, and concatenate it with the original string.
7th Aug 2017, 1:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
I looked into this when researching the 'endl' macro/ template (to show why newlines are faster than endl). All the operator overload options: http://en.cppreference.com/w/cpp/language/operators For one we've all seen, cout uses overloaded operators... see the definition for << (insertion): http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/ (the %3C are for <<, otherwise SoloLearn breaks the link) Notice all the types it can handle. I believe you can make more by adding a new signature (and + is one of the 38 supported operators)
7th Aug 2017, 3:55 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Just fixed the link with url (%) encoding, sorry about that.
7th Aug 2017, 4:02 AM
Kirk Schafer
Kirk Schafer - avatar
- 1
string s = string("hi") + 5; What should s look like? "hi5"? "hi " 5 spaces? Possible, but why?
9th Aug 2017, 10:28 AM
Timon Paßlick
- 1
yes it a matter of converting the numerical value to string .
1st Oct 2017, 3:19 AM
ankamah victor
- 1
Ostias, lo no sé tio :,v
1st Oct 2017, 4:53 AM
ALEJANDRO PUENTE GONZALEZ
ALEJANDRO PUENTE GONZALEZ - avatar
- 1
Hello there I'm new here
30th Mar 2020, 2:11 PM
Sim
Sim - avatar
- 1
I have questions
2nd Apr 2020, 3:50 PM
Yair Daniel Limón Hernández
Yair Daniel Limón Hernández - avatar
- 1
can you help me out
5th Oct 2020, 7:56 PM
ttvclvckpwner
ttvclvckpwner - avatar
- 1
Alch no hablo inglés brow jsjsjajajaja
28th Apr 2021, 2:26 PM
Diego Morales
Diego Morales - avatar
- 1
Posted in the machine
6th Sep 2021, 10:59 AM
Elena IVANOVA
Elena IVANOVA - avatar
- 1
Tengo hambre
11th Nov 2021, 12:50 PM
Miguel Vasquez
- 1
*Complete Achievements*
25th Feb 2022, 10:12 PM
Иса Мусаев
Иса Мусаев - avatar
- 4
wow cool! Thanks Kirk!
7th Aug 2017, 3:57 AM
Karl
Karl - avatar