c++重载运算符两种形式的选择
Posted soak xiarl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++重载运算符两种形式的选择相关的知识,希望对你有一定的参考价值。
可重载的操作符,摘录自c++ primer第四版 表14.1:
+ |
- |
* |
/ |
% |
^ |
& |
| |
~ |
! |
, |
= |
< |
> |
<= |
>= |
++ |
-- |
<< |
>> |
== |
!= |
&& |
|| |
+= |
-= |
/= |
%= |
^= |
&= |
|= |
*= |
<<= |
>>= |
[] |
() |
-> |
->* |
new |
new [] |
delete |
delete [] |
c++中可以重载运算符,有两种实现形式,成员函数和非成员函数。
如何选择,之前记得有本书里直接有个推荐形式的表,没找到。先把《exceptional c++》第20条中写的文字摘录下来:
The standard requires that operators = () [] and -> must be members, and class-specific operators new, new[], delete, and delete[] must be static members.
For all other functions:
if the function is operator>> or operator<< for stream I/O,
or if it needs type conversions on its leftmost argument,
or if it can be implemented using the class\'s public interface alone,
make it a nonmember (and friend if needed in the first two cases)
if it needs to behave virtually,
add a virtual member function to provide the virtual behavior, and implement it in terms of that
else
make it a member
看了这个描述,感觉还是要把那个表找到啊。。。
以上是关于c++重载运算符两种形式的选择的主要内容,如果未能解决你的问题,请参考以下文章