由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - operator overloading (C++)
相关主题
why use static function here?static function and static variable?
tempalte as the overloaded conversion operator有没有static return type和static as function arguement?
程序怎么不认识class, namespace等identifier呀?How to check the virtual function table size?
c++ 语法a C++ question
Any difference between class and typename identifier?i +++ j
问两个问题,C++access function static variable
C++: operator new 为啥要是 static的, 不是有啥影响?javascript function-ask for help
求教new delete 在c++中的重载What syntax is this?(about macro)
相关话题的讨论汇总
话题: operator话题: function话题: c++话题: static
进入Programming版参与讨论
1 (共1页)
G****A
发帖数: 4160
1
Can I claim an operator overloading function as a static member function?
What are the pros and cons for doing that, compared with a non-static
operator overloading member function?
X****r
发帖数: 3557
2
No.

【在 G****A 的大作中提到】
: Can I claim an operator overloading function as a static member function?
: What are the pros and cons for doing that, compared with a non-static
: operator overloading member function?

G****A
发帖数: 4160
3
Thanks for reply, but is there any specific reason?
The intuition of my question is that an operator, say "<<", should be
associated with the class (independent of one particular object), which
matches well the definition of static member function.

【在 X****r 的大作中提到】
: No.
X****r
发帖数: 3557
4
Because you can simply define a function to overload the operator
in global scope (and put it in the same source file with your class
if you want).

【在 G****A 的大作中提到】
: Thanks for reply, but is there any specific reason?
: The intuition of my question is that an operator, say "<<", should be
: associated with the class (independent of one particular object), which
: matches well the definition of static member function.

G****A
发帖数: 4160
5
thanks

【在 X****r 的大作中提到】
: Because you can simply define a function to overload the operator
: in global scope (and put it in the same source file with your class
: if you want).

d****p
发帖数: 685
6
1. Standard explicitly specifies this.(clause 13.5.6)
2. The purpose of overloaded operator is to mimic built-in operator syntax.
If you define your operator as class static member function (supposing
standard allows), you got to invoke it as ::operator operator identifier>(argument list), which looks so esoteric compared with
the way invoking native operator.

【在 G****A 的大作中提到】
: Thanks for reply, but is there any specific reason?
: The intuition of my question is that an operator, say "<<", should be
: associated with the class (independent of one particular object), which
: matches well the definition of static member function.

G****A
发帖数: 4160
7
(2) makes sense.

syntax.
with

【在 d****p 的大作中提到】
: 1. Standard explicitly specifies this.(clause 13.5.6)
: 2. The purpose of overloaded operator is to mimic built-in operator syntax.
: If you define your operator as class static member function (supposing
: standard allows), you got to invoke it as ::operator: operator identifier>(argument list), which looks so esoteric compared with
: the way invoking native operator.

G****A
发帖数: 4160
8
then how about the situation where two classes nest at one source file
and each class requires its own operator overloading?


【在 X****r 的大作中提到】
: Because you can simply define a function to overload the operator
: in global scope (and put it in the same source file with your class
: if you want).

X****r
发帖数: 3557
9
What's the problem then? Each class overloads the same operator
in the global space. These overloaded functions differ by their
parameter list. This is what 'overloading' supposes to do.

【在 G****A 的大作中提到】
: then how about the situation where two classes nest at one source file
: and each class requires its own operator overloading?
:

1 (共1页)
进入Programming版参与讨论
相关主题
What syntax is this?(about macro)Any difference between class and typename identifier?
The future of Javascript module management finally comes问两个问题,C++
boost::function 的 syntax 问题C++: operator new 为啥要是 static的, 不是有啥影响?
expert level C++求教new delete 在c++中的重载
why use static function here?static function and static variable?
tempalte as the overloaded conversion operator有没有static return type和static as function arguement?
程序怎么不认识class, namespace等identifier呀?How to check the virtual function table size?
c++ 语法a C++ question
相关话题的讨论汇总
话题: operator话题: function话题: c++话题: static