c++中operator可以和什么运算符搭配使用
时间: 2023-05-27 09:05:05 浏览: 93
在C语言中,operator可以和以下运算符搭配使用:
1. 算术运算符:+、-、*、/、%(取余数)
2. 比较运算符:<、>、<=、>=、==、!=
3. 逻辑运算符:&&(逻辑与)、||(逻辑或)、!(逻辑非)
4. 位运算符:&(按位与)、|(按位或)、^(按位异或)、~(按位取反)、<<(左移位)、>>(右移位)
5. 赋值运算符:=、+=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=
6. 其他运算符:sizeof(获取变量所占字节数)、?:(三目运算符,类似于if-else语句)
相关问题
c++在结构体内operator重载的运算符使用范围
在结构体内定义的operator重载的运算符可以被用于该结构体的对象之间的操作。具体来说,它可以被用于两个该结构体类型的对象之间的操作,或者一个该结构体类型的对象和一个与其兼容的其他类型的对象之间的操作。例如,如果在一个名为MyStruct的结构体中定义了一个operator+运算符,那么它可以被用于两个MyStruct类型的对象之间进行加法操作,也可以被用于一个MyStruct类型的对象和一个与其兼容的其他类型的对象之间进行加法操作。
C++中的运算符都可以作为什么函数重载
在C++中,几乎所有的运算符都可以被重载为成员函数或非成员函数。下面是可以被重载和其对应的重载函数的形式:
1. 算术运算符:
```cpp
// 类成员函数形式
T operator+(const T& obj) const;
T operator-(const T& obj) const;
T operator*(const T& obj) const;
T operator/(const T& obj) const;
T operator%(const T& obj) const;
// 非成员函数形式
T operator+(const T& obj1, const T& obj2);
T operator-(const T& obj1, const T& obj2);
T operator*(const T& obj1, const T& obj2);
T operator/(const T& obj1, const T& obj2);
T operator%(const T& obj1, const T& obj2);
```
2. 关系运算符:
```cpp
// 类成员函数形式
bool operator==(const T& obj) const;
bool operator!=(const T& obj) const;
bool operator<(const T& obj) const;
bool operator>(const T& obj) const;
bool operator<=(const T& obj) const;
bool operator>=(const T& obj) const;
// 非成员函数形式
bool operator==(const T& obj1, const T& obj2);
bool operator!=(const T& obj1, const T& obj2);
bool operator<(const T& obj1, const T& obj2);
bool operator>(const T& obj1, const T& obj2);
bool operator<=(const T& obj1, const T& obj2);
bool operator>=(const T& obj1, const T& obj2);
```
3. 赋值运算符:
```cpp
// 类成员函数形式
T& operator=(const T& obj);
// 非成员函数形式
T& operator=(T& obj1, const T& obj2);
```
4. 自增/自减运算符:
```cpp
// 类成员函数形式
T& operator++();
T& operator--();
T operator++(int);
T operator--(int);
// 非成员函数形式
T& operator++(T& obj);
T& operator--(T& obj);
T operator++(T& obj, int);
T operator--(T& obj, int);
```
5. 成员访问运算符:
```cpp
// 类成员函数形式
T& operator[](int index);
const T& operator[](int index) const;
// 非成员函数形式
T& operator[](T& obj, int index);
const T& operator[](const T& obj, int index);
```
6. 函数调用运算符:
```cpp
// 类成员函数形式
T operator()(args...);
// 非成员函数形式
T operator()(T& obj, args...);
```
7. 类型转换运算符:
```cpp
// 类成员函数形式
operator type();
// 非成员函数形式
operator type(T& obj);
```
其中,`T` 表示类名或类型名,`args` 表示参数列表,`type` 表示目标类型。可以根据需要选择合适的重载函数的形式进行重载。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)