c++重载比较符号的格式
时间: 2023-12-11 12:02:27 浏览: 24
C++中可以重载比较运算符,包括小于号(<)、大于号(>)、小于等于号(<=)、大于等于号(>=)、等于号(==)和不等于号(!=)。其格式如下:
```c++
bool operator< (const MyClass& obj) const {
// 比较逻辑,返回 bool 类型
}
```
其中,`operator<`代表重载的运算符,`MyClass`是自定义类的名称,`obj`是一个常引用类型的对象,`const`表示该函数不会修改类的成员变量。
需要注意的是,重载运算符 `<` 时,通常需要同时重载运算符 `>`、`<=`、`>=` 和 `!=`。这些运算符的重载格式类似,只需将 `<` 替换为相应的符号即可。
相关问题
c++重载运算符技巧
C++中的运算符重载是一种特殊的多态性,它允许我们重新定义已有的运算符,以便用于用户自定义的数据类型。以下是一些C++重载运算符的技巧:
1. 运算符重载函数必须是类的成员函数或全局函数,但不能是普通的成员函数。
2. 重载运算符函数的名称必须是operator后面跟着要重载的运算符符号,例如+、-、*、/等。
3. 重载运算符函数可以有任意数量的参数,但必须至少有一个参数是类类型或枚举类型。
4. 重载运算符函数可以返回任何类型的值,但通常返回一个新的类对象。
5. 重载运算符函数可以是const函数,这意味着它不会修改类的成员变量。
6. 重载运算符函数可以是友元函数,这意味着它可以访问类的私有成员变量。
以下是一个C++重载运算符的例子:
```cpp
#include <iostream>
using namespace std;
class Complex {
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& c) const {
return Complex(real + c.real, imag + c.imag);
}
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.real << "+" << c.imag << "i";
return os;
}
private:
double real;
double imag;
};
int main() {
Complex c1(1.0, 2.0);
Complex c2(3.0, 4.0);
Complex c3 = c1 + c2;
cout << c3 << endl; // 输出:4+6i
return 0;
}
```
C++重载运算符
C++中的运算符重载是一种特殊的函数重载,允许我们重新定义已有的运算符,使其能够用于自定义类型。运算符重载函数的名称必须是 `operator` 关键字后跟要重载的运算符的符号。
以下是一些常用的运算符及其重载函数的示例:
### 1. 算术运算符
| 运算符 | 函数名称 |
|-------|---------|
| + | operator+ |
| - | operator- |
| * | operator* |
| / | operator/ |
| % | operator% |
```cpp
class Number {
public:
int value;
Number(int v) : value(v) {}
Number operator+(const Number& other) {
return Number(value + other.value);
}
Number operator-(const Number& other) {
return Number(value - other.value);
}
Number operator*(const Number& other) {
return Number(value * other.value);
}
Number operator/(const Number& other) {
return Number(value / other.value);
}
Number operator%(const Number& other) {
return Number(value % other.value);
}
};
```
### 2. 比较运算符
| 运算符 | 函数名称 |
|-------|---------|
| == | operator== |
| != | operator!= |
| < | operator< |
| > | operator> |
| <= | operator<= |
| >= | operator>= |
```cpp
class Person {
public:
std::string name;
int age;
Person(const std::string& n, int a) : name(n), age(a) {}
bool operator==(const Person& other) {
return name == other.name && age == other.age;
}
bool operator!=(const Person& other) {
return !(*this == other);
}
bool operator<(const Person& other) {
return age < other.age;
}
bool operator>(const Person& other) {
return age > other.age;
}
bool operator<=(const Person& other) {
return age <= other.age;
}
bool operator>=(const Person& other) {
return age >= other.age;
}
};
```
### 3. 位运算符
| 运算符 | 函数名称 |
|-------|---------|
| & | operator& |
| \| | operator\| |
| ^ | operator^ |
| ~ | operator~ |
| << | operator<< |
| >> | operator>> |
```cpp
class Bitset {
private:
int data;
public:
Bitset(int d) : data(d) {}
Bitset operator&(const Bitset& other) {
return Bitset(data & other.data);
}
Bitset operator|(const Bitset& other) {
return Bitset(data | other.data);
}
Bitset operator^(const Bitset& other) {
return Bitset(data ^ other.data);
}
Bitset operator~() {
return Bitset(~data);
}
Bitset operator<<(int shift) {
return Bitset(data << shift);
}
Bitset operator>>(int shift) {
return Bitset(data >> shift);
}
};
```