C++: string operator overload

后端 未结 3 1208
生来不讨喜
生来不讨喜 2021-01-21 14:00

Can I overload existing function/operator in existing class?

I was trying to do:

#include 
#include 
using namespace std;

         


        
3条回答
  •  执念已碎
    2021-01-21 14:34

    You can't add member functions to a class unless you modify that class' definition. Use a free function instead:

    string& operator<<(string & lhs, const string & rhs) {
        return lhs += rhs;
    }
    

提交回复
热议问题