由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ operator overloading question
相关主题
请问一个implicit conversion的问题(C++)问个overloading new operator的问题
C++ Q02:一个inheritance 的问题
stl Compare为何需要重载()?question overloading ++ error
An object of A automatically converted to an object of B.one question about overloading operator delete
小问题这段code有啥问题?
question regarding const functionHow to overload global new operator?
tempalte as the overloaded conversion operatoroperator overloading (C++)
why use static function here?请问关于overloading <<
相关话题的讨论汇总
话题: operator话题: type话题: return话题: c++
进入Programming版参与讨论
1 (共1页)
j******6
发帖数: 146
1
sometime I see
T& operator() {...}
another time I see
operator T*() {...}
why the keyword "operator" sometimes is ahead of the return type
and sometimes it is after that?
BTW,
why are these kind of functions are called implicitly?
M*********t
发帖数: 257
2
The 2nd case you mentioned is actually quite special
I don't think it is a real operator function at all.
It is a special member function to achieve implicit type
conversion (with operator keyword but no return type)

【在 j******6 的大作中提到】
: sometime I see
: T& operator() {...}
: another time I see
: operator T*() {...}
: why the keyword "operator" sometimes is ahead of the return type
: and sometimes it is after that?
: BTW,
: why are these kind of functions are called implicitly?

t****t
发帖数: 6806
3
T& operator()( ... ) [you missed the 2nd paren] means operator(). for
object a, a(1, 2, 3) calls operator(). return type is T&.
operator T*() means operator to convert type to T*. the return type must be
T* so it is omitted. for object a, (T*)a calls operator T*.

【在 j******6 的大作中提到】
: sometime I see
: T& operator() {...}
: another time I see
: operator T*() {...}
: why the keyword "operator" sometimes is ahead of the return type
: and sometimes it is after that?
: BTW,
: why are these kind of functions are called implicitly?

a***y
发帖数: 2803
4
第一个operator是().类似的还有[],它也可以overload.
第二个operator是T*.

【在 j******6 的大作中提到】
: sometime I see
: T& operator() {...}
: another time I see
: operator T*() {...}
: why the keyword "operator" sometimes is ahead of the return type
: and sometimes it is after that?
: BTW,
: why are these kind of functions are called implicitly?

m*4
发帖数: 1341
5
第一个是在overload 圆括弧()这个operator, return type 是 T&. 实际上是在产生一
个"functor", 挺常见的。
第二个实际上是在overload conversion operator.
比如,你可以写:
operator int(){}
conversion operator declare 的时候不能有return type, 不能带argument, 必须是
member function.
属于很生冷的用法了。

【在 j******6 的大作中提到】
: sometime I see
: T& operator() {...}
: another time I see
: operator T*() {...}
: why the keyword "operator" sometimes is ahead of the return type
: and sometimes it is after that?
: BTW,
: why are these kind of functions are called implicitly?

1 (共1页)
进入Programming版参与讨论
相关主题
请问关于overloading <<小问题
问题的根源找到了question regarding const function
Go不支持operator overloadtempalte as the overloaded conversion operator
大牛对Scala的type system如何评价?why use static function here?
请问一个implicit conversion的问题(C++)问个overloading new operator的问题
C++ Q02:一个inheritance 的问题
stl Compare为何需要重载()?question overloading ++ error
An object of A automatically converted to an object of B.one question about overloading operator delete
相关话题的讨论汇总
话题: operator话题: type话题: return话题: c++