码迷,mamicode.com
首页 > 编程语言 > 详细

c++重载运算符两种形式的选择

时间:2021-06-06 18:47:16      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:直接   ext   primer   res   public   except   style   static   first   

 

可重载的操作符,摘录自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++重载运算符两种形式的选择

标签:直接   ext   primer   res   public   except   style   static   first   

原文地址:https://www.cnblogs.com/xiarl/p/14853696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!