You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
933 B
Markdown
33 lines
933 B
Markdown
2 years ago
|
---
|
||
|
icon: edit
|
||
|
date: 2023-01-11
|
||
|
category:
|
||
|
- 系统配置
|
||
|
- cpp
|
||
|
tag:
|
||
|
- 操作符
|
||
|
- 重载
|
||
|
headerDepth: 5
|
||
|
---
|
||
|
|
||
|
|
||
|
# c++ 能重载的操作符有那些
|
||
|
重载操作符是指重新定义C++中已有运算符的含义。常见的重载操作符有:
|
||
|
```
|
||
|
算术运算符: +, -, *, /, %
|
||
|
关系运算符: ==, !=, >, <, >=, <=
|
||
|
逻辑运算符: &&, ||, !
|
||
|
赋值运算符: =
|
||
|
位运算符: &, |, ^, ~, <<, >>
|
||
|
自增/自减运算符: ++, --
|
||
|
成员访问运算符: ->, .
|
||
|
下标运算符: [ ]
|
||
|
函数调用运算符: ( )
|
||
|
转型运算符: (type)
|
||
|
new 和 delete 运算符: new, delete, new[], delete[]
|
||
|
另外还有三个特殊的运算符是可以被重载的:
|
||
|
复合赋值运算符: +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
|
||
|
串联运算符: <<, >>
|
||
|
逗号运算符: ,
|
||
|
```
|
||
|
需要注意的是所有运算符重载都需要在类内部进行,且实现的时候可能需要使用特殊的关键字来实现,如 ```friend``` 和 ```operator```.
|