运算重载符不可以改变语法结构、不可以改变操作数的个数、不可以改变优先级、不可以改变结合性。
自定义类Object,重载运算符:
运算符 | 说明 | 成员函数原型 | 全局函数原型 |
前++ | 自身加1 | Object& Object::operator ++ () | |
后++ | 自身加1 | Object Object::operator ++ (int) | |
前-- | 自身减1 | Object& Object::operator -- () | |
后-- | 自身减1 | Object Object::operator -- (int) | |
+ | 加 | Object Object::operator + (const Object&) | Object operator + (const Object&, const Object&) |
- | 减 | Object Object::operator - (const Object&) | Object operator - (const Object&, const Object&) |
* | 乘 | Object Object::operator * (const Object&) | Object operator * (const Object&, const Object&) |
/ | 除 | Object Object::operator / (const Object&) | Object operator / (const Object&, const Object&) |
% | 取余 | Object Object::operator % (const Object&) | Object operator % (const Object&, const Object&) |
- | 负号 | Object operator - () |
逻辑运算符
运算符 | 说明 | 成员函数原型 | 全局函数原型 |
&& | 且 | bool Object::operator && (const Object&) | Object operator && (const Object&, const Object&) |
|| | 或 | bool Object::operator || (const Object&) | Object operator || (const Object&, const Object&) |
! | 逻辑非 | bool Object::operator ! () |
位运算符
运算符 | 说明 | 成员函数原型 | 全局函数原型 |
& | 位且 | Object Object::operator & (const Object&) | Object operator & (const Object&, const Object&) |
| | 位或 | Object Object::operator | (const Object&) | Object operator | (const Object&, const Object&) |
^ | 异或 | Object Object::operator ^ (const Object&) | Object operator ^ (const Object&, const Object&) |
~ | 按位取反 | Object Object::operator ~ () | |
<< | 左移 | Object Object::operator << (int) | Object operator << (const Object&, int) |
>> | 右移 | Object Object::operator >> (int) | Object operator >> (const Object&, int) |
关系运算符
运算符 | 说明 | 成员函数原型 | 全局函数原型 |
= | 赋值 | Object& Object::operator = (const Object& other) | |
== | 相等 | bool Object::operator == (const Object& other) | bool operator == (const Object&, const Object&) |
!= | 不相等 | bool Object::operator != (const Object& other) | bool operator != (const Object&, const Object&) |
< | 小于 | bool Object::operator < (const Object& other) | bool operator < (const Object&, const Object&) |
<= | 小于等于 | bool Object::operator <= (const Object& other) | bool operator <= (const Object&, const Object&) |
> | 大于 | bool Object::operator > (const Object& other) | bool operator > (const Object&, const Object&) |
>= | 大于等于 | bool Object::operator >= (const Object& other) | bool operator >= (const Object&, const Object&) |
成员运算符
运算符 | 说明 | 成员函数原型 | 全局函数原型 |
-> | 指针,可用于智能指针的封装,Type为其他类型 | Type* Object::operator -> () |
原文链接: https://www.cnblogs.com/tianrks/p/10932197.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/296080
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!