r*******y 发帖数: 1081 | 1 string library defines these overloaded operator +
string operator+ (const string& lhs, const string& rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (const string& lhs, char rhs);
We know
string s = "hi" + "ji";
is not OK
If compiling this using g++ , I get the error :
invalid operands of types ‘const char [3]’ and ‘const char [3]’ to
binary ‘operator+’
I am a little confused about the error message. I think it is wrong because
the
function call is ambiguious. We can convert one or both to a string type and
then call one overloaded operator +, so it is ambiguous.
Why the complier complains about invalide operands instead of ambiguous call
?
thanks. | t****t 发帖数: 6806 | 2 overloaded operator must have at least one user-defined operand. you can't
overload operator for 2 built-in operands.
【在 r*******y 的大作中提到】 : string library defines these overloaded operator + : string operator+ (const string& lhs, const string& rhs); : string operator+ (const char* lhs, const string& rhs); : string operator+ (char lhs, const string& rhs); : string operator+ (const string& lhs, const char* rhs); : string operator+ (const string& lhs, char rhs); : We know : string s = "hi" + "ji"; : is not OK : If compiling this using g++ , I get the error :
| r*******y 发帖数: 1081 | 3 good point. thanks a lot
【在 t****t 的大作中提到】 : overloaded operator must have at least one user-defined operand. you can't : overload operator for 2 built-in operands.
| a***y 发帖数: 2803 | 4 string s = "hi" + "ji";
是不是left operand,right operand都是const char*,在5个函数里都没有这个
signature?
"At least one of the arguments used has to be a string object."
参考:
http://www.cplusplus.com/reference/string/operator+/
Parameters
lhs
A string object, a c-string or a character. Its content forms the
beginning of the returned object.
rhs
If lhs is a string object, this is another string object, a c-string or
a character.
If lhs is not a string object, this is a string object.
In either case, the content of rhs follows that of lhs in the returned
object.
Notice that at least one of the arguments used has to be a string object.
【在 r*******y 的大作中提到】 : string library defines these overloaded operator + : string operator+ (const string& lhs, const string& rhs); : string operator+ (const char* lhs, const string& rhs); : string operator+ (char lhs, const string& rhs); : string operator+ (const string& lhs, const char* rhs); : string operator+ (const string& lhs, char rhs); : We know : string s = "hi" + "ji"; : is not OK : If compiling this using g++ , I get the error :
| t****t 发帖数: 6806 | 5
wrong answer.
correct direction. to be precise, at least one of the operands has to be
user-defined.
【在 a***y 的大作中提到】 : string s = "hi" + "ji"; : 是不是left operand,right operand都是const char*,在5个函数里都没有这个 : signature? : "At least one of the arguments used has to be a string object." : 参考: : http://www.cplusplus.com/reference/string/operator+/ : Parameters : lhs : A string object, a c-string or a character. Its content forms the : beginning of the returned object.
| M*********t 发帖数: 257 | 6 Since both argument expression are of const char* type
why the compiler doesn't do an automatic type conversion to convert
one of the argument to type const string&, Isn't there a
constructor in string class that enable this type conversion?
Two possible reasons
(1) Is this due to the reference symbol &, that prevents this conversion (
can't convert to a reference type?)
(2) Due to ambiguity ie. compiler doesn't know whether to convert 1st or 2nd
argument to match an overloaded operator function (both version exist).
Which one is more likely?
【在 t****t 的大作中提到】 : : wrong answer. : correct direction. to be precise, at least one of the operands has to be : user-defined.
| t****t 发帖数: 6806 | 7 why is it so difficult to understand? you MUST have at least one user-
defined operand, such that the compiler will start to consider using
overloaded operators.
is my english that bad?
2nd
【在 M*********t 的大作中提到】 : Since both argument expression are of const char* type : why the compiler doesn't do an automatic type conversion to convert : one of the argument to type const string&, Isn't there a : constructor in string class that enable this type conversion? : Two possible reasons : (1) Is this due to the reference symbol &, that prevents this conversion ( : can't convert to a reference type?) : (2) Due to ambiguity ie. compiler doesn't know whether to convert 1st or 2nd : argument to match an overloaded operator function (both version exist). : Which one is more likely?
| M*********t 发帖数: 257 | 8 I am actually confused about what you mean by user-defined operand.
if I understand correctly you mean for a simple expression like a+b
both a and b must be of user defined type in order for a compiler
to consider using overloaded operator functions.
But for builtin type like string there are also a list of overloaded
operator functions for + (as LZ listed out)
Either your concept of operator overloading is wrong or I misunderstood what
you mean by user-defined operand.
【在 t****t 的大作中提到】 : why is it so difficult to understand? you MUST have at least one user- : defined operand, such that the compiler will start to consider using : overloaded operators. : is my english that bad? : : 2nd
| t****t 发帖数: 6806 | 9 user-defined type means class or struct, as opposed to built-in type such as
int, char, pointer.
string, is not a built-in type.
what
【在 M*********t 的大作中提到】 : I am actually confused about what you mean by user-defined operand. : if I understand correctly you mean for a simple expression like a+b : both a and b must be of user defined type in order for a compiler : to consider using overloaded operator functions. : But for builtin type like string there are also a list of overloaded : operator functions for + (as LZ listed out) : Either your concept of operator overloading is wrong or I misunderstood what : you mean by user-defined operand.
| M*********t 发帖数: 257 | 10 Thanks, I see what you mean. string is a class type supplied by STL
so it is not a built in type,string literal like "abc" is const char*
a builtin type.
but my earlier point is rather than refuse to consider using overloaded
operator,compiler should use implicit type conversion to turn arguments into
class type and then try to match with one of the overloaded function.
This process is so called overloading resolution. Why this rule doesn't
apply here for operator function with String type, Is string special?
as
【在 t****t 的大作中提到】 : user-defined type means class or struct, as opposed to built-in type such as : int, char, pointer. : string, is not a built-in type. : : what
| t****t 发帖数: 6806 | 11 first of all, it is not up to you or me to say compiler "should" do what and
what. a rule is there and it applies, that's it.
second, why it is designed this way? suppose in addition to std::string, you
define another class A that can converts const char* implicitly, and also
defines operator+(A, A). now you write "abc"+"def". what does it mean? it is
not clear. it can mean string("abc")+string("def"), or means A("abc")+A("
def"), or string("abc")+A("def"), etc. basically a mess. compiler must
guarantee that, when no user-defined class is involved, everything is clear
and definite.
into
【在 M*********t 的大作中提到】 : Thanks, I see what you mean. string is a class type supplied by STL : so it is not a built in type,string literal like "abc" is const char* : a builtin type. : but my earlier point is rather than refuse to consider using overloaded : operator,compiler should use implicit type conversion to turn arguments into : class type and then try to match with one of the overloaded function. : This process is so called overloading resolution. Why this rule doesn't : apply here for operator function with String type, Is string special? : : as
|
|