简体   繁体   中英

operator overloading c++ for non-friend class

I tried to implement the following function

std::string operator+(std::string s, int t);

but it doesn't get called if I try

std::string s = "abc" + 123;

although it gets called if I call

operator+("abc", 123);

Is there something I am missing?

"abc" is not a std::string , but a const char[] . You could try:

std::string s = std::string("abc") + 123;

Or, if you are using C++14,

using namespace std::string_literals;
std::string s = "abc"s + 123;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM