If I have a string x='wow' in Python, I can concatenate this string with itself using the __add__ function, like so:
x='wow'
x.__add__(x)
'wowwow'
How can I do this in C++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Semantically, the equivalent of your python code would be something like
i.e. create a temporary string which is the concatenation of
xwithxand throw away the result. To append toxyou would do the following:Note the double quotes
". Unlike python, in C++, single quotes are for single characters, and double-quotes for null terminated string literals.See this
std::stringreference.By the way, in Python you wouldn’t usually call the
__add__()method. You would use the equivalent syntax to the first C++ example:The
__add__()method is just the python way of providing a “plus” operator for a class.