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?
|