@src: http://www.java2s.com/Code/Cpp/Overload/Friendlyoperator.htm make -k main && ./main g++ main.cpp -o main main.cpp: In function 'int main()': main.cpp:158: warning: deprecated conversion from string constant to 'char*' main.cpp:170: error: ambiguous overload for 'operator+' in 'c1 + s2' main.cpp:139: note: candidates are: n::String operator+(const n::String&, const n::String&) main.cpp:35: note: n::String n::operator+(const n::String&, const n::String&) #include #define LOGf(f,...) printf( f "\n", __VA_ARGS__ ) //std #define LOG() LOGf("%s:%d: %s ### ", __FILE__, __LINE__ , __PRETTY_FUNCTION__) #include #include using namespace std; #if 1 # define SUPPORT_NAMESPACE 1 # define SUPPORT_FRIEND 1 # define SUPPORT_INLINE 0 # define SUPPORT_OP 1 #elif 0 # define SUPPORT_NAMESPACE 1 # define SUPPORT_FRIEND 1 # define SUPPORT_INLINE 0 # define SUPPORT_OP 1 #elif 0 # define SUPPORT_NAMESPACE 1 # define SUPPORT_FRIEND 1 # define SUPPORT_INLINE 1 # define SUPPORT_OP 1 #elif 0 // bug # define SUPPORT_NAMESPACE 1 // uncomment to for bug # define SUPPORT_FRIEND // main.cpp:181: error: ambiguous overload for 'operator+' in 's1 + s2' // main.cpp:148: note: candidates are: n::String operator+(const n::String&, const n::String&) // main.cpp:44: note: n::String n::operator+(const n::String&, const n::String&) #elif 0 //working # define SUPPORT_NAMESPACE # define SUPPORT_OP #elif 1 // working # define SUPPORT_FRIEND # define SUPPORT_OP #endif #if SUPPORT_NAMESPACE namespace n { #endif class String; String operator+(const String&, const String&); class String{ public: String(); String(const char *const); String(const String &); ~String(); char & operator[](int offset); char operator[](int offset) const; #ifdef SUPPORT_OP String operator+(const String&) /*const*/; #endif #if (SUPPORT_FRIEND) #if ! SUPPORT_INLINE friend String operator+(const String&, const String&); #else friend String operator+(const String & lhs, const String& rhs) { LOG(); int totalLen = lhs.GetLen() + rhs.GetLen(); n::String temp(totalLen); int i, j; for (i = 0; i itsLen) return itsString[itsLen-1]; else return itsString[offset]; } char String::operator[](int offset) const { if (offset > itsLen) return itsString[itsLen-1]; else return itsString[offset]; } #ifdef SUPPORT_OP String String::operator+(const String& rhs) /*const*/ { LOG(); int totalLen = itsLen + rhs.GetLen(); String temp(totalLen); int i, j; for (i = 0; i {{http://i160.photobucket.com/albums/t193/miggetymary/Morticia-Addams.jpg}}