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