由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - string operator +
相关主题
请问关于overloading <<一个c++小问题
C++命名空间和算子重载int 和 unsigned int 比大小会出问题 c++
member and friend问个函数指针指向操作符的问题
overriding operator<<C语言里面的register变量能否进行取地址操作? (转载)
问个c++的弱问题(面试题) 给code挑毛病(updated with multiple choices)
有个SB interviewer和我说++i比i++好 (转载)问几句汇编指令(assembly language)
0 < -1 ? A c++ questionwhy use static function here?
为啥gcc找不到类的构造函数?问个overloading new operator的问题
相关话题的讨论汇总
话题: string话题: operator话题: const话题: lhs话题: char
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
问个overloading new operator的问题问个c++的弱问题
ambiguous operators in c++有个SB interviewer和我说++i比i++好 (转载)
面试问题0 < -1 ? A c++ question
question overloading ++ error为啥gcc找不到类的构造函数?
请问关于overloading <<一个c++小问题
C++命名空间和算子重载int 和 unsigned int 比大小会出问题 c++
member and friend问个函数指针指向操作符的问题
overriding operator<<C语言里面的register变量能否进行取地址操作? (转载)
相关话题的讨论汇总
话题: string话题: operator话题: const话题: lhs话题: char