对已有的运算符重新定义,赋予另一种功能,以使用不同数据类型
对于内置数据类型表达式运算符,不可改变 例如 int a = 1 + 1;
不可改变
关键字operator
加法运算符重载
代码示例
#include <iostream>
using namespace std;
class Person {
public:
int age;
int weight;
// 声明为友元函数以便访问私有成员(如果成员变成private)
friend ostream& operator<<(ostream& os, const Person& p);
};
// 重载加法运算符
Person operator+(const Person &p1, const Person &p2) {
Person temp;
temp.age = p1.age + p2.age;
temp.weight = p1.weight + p2.weight;
return temp;
}
// 左移运算符重载实现
ostream& operator<<(ostream& os, const Person& p) {
os << "Person(age: " << p.age << ", weight: " << p.weight << ")";
return os; // 返回ostream引用以支持链式调用
}
Person operator+(const Person &p1, const Int num) {
Person temp;
temp.age = p1.age + num;
temp.weight = p1.weight + num;
return temp;
}
int main() {
Person p1, p2;
p1.age = 10; // 这些赋值语句
p2.age = 10; // 必须放在
p1.weight = 100;// 函数内部
p2.weight = 100;
Person p3 = p1 + p2;
// 测试输出
cout << "p3.age: " << p3.age << ", weight: " << p3.weight << endl;
// 输出: p3.age: 20, weight: 200
Person p4 = p1 + 10;
cout << "p4.age: " << p4.age << ", weight: " << p4.weight << endl;
// 输出: p4.age: 20, weight: 110
return 0;
}
通过成员函数重载
class Person {
public:
// ... 成员变量 ...
Person operator+(const Person &other) const {
Person temp;
temp.age = age + other.age;
temp.weight = weight + other.weight;
return temp;
}
};
全局函数重载本质是operator+(p1,p2)
成员函数的重载本质是p1.operator+(p2)
递增运算符重载
#include <iostream>
using namespace std;
class Person {
public:
int age;
int weight;
// 构造函数
Person(int a = 0, int w = 0) : age(a), weight(w) {}
// 前置递增运算符重载
Person& operator++() {
cout << "调用前置递增\n";
++age;
++weight;
return *this;
}
// 后置递增运算符重载
Person operator++(int) {
cout << "调用后置递增\n";
Person temp = *this;
++(*this); // 调用前置递增实现
return temp;
}
// 输出运算符重载
friend ostream& operator<<(ostream& os, const Person& p) {
os << "Person(age: " << p.age << ", weight: " << p.weight << ")";
return os;
}
};
int main() {
Person p1(25, 70);
cout << "初始p1: " << p1 << endl;
// 测试前置递增
cout << "\n前置递增测试:\n";
Person p2 = ++p1; // 等价于 p1.operator++()
cout << "p1: " << p1 << endl;
cout << "p2: " << p2 << endl;
// 测试后置递增
cout << "\n后置递增测试:\n";
Person p3 = p1++; // 等价于 p1.operator++(0)
cout << "p1: " << p1 << endl;
cout << "p3: " << p3 << endl;
// 链式调用测试
cout << "\n链式调用测试:\n";
Person p4(30, 80);
cout << "原始p4: " << p4 << endl;
cout << "链式调用: " << ++(++p4) << endl;
cout << "最终p4: " << p4 << endl;
return 0;
}
赋值运算符的重载
#include <iostream>
#include <cstring> // for memcpy
class MyString {
private:
char* data;
size_t length;
public:
// 构造函数
MyString(const char* str = "") {
length = std::strlen(str);
data = new char[length + 1];
std::strcpy(data, str);
}
// 析构函数
~MyString() {
delete[] data;
}
// 赋值运算符重载
MyString& operator=(const MyString& other) {
// 1. 检查自赋值
if (this == &other) {
return *this;
}
// 2. 释放当前资源
delete[] data;
// 3. 分配新资源
length = other.length;
data = new char[length + 1];
// 4. 复制内容
std::strcpy(data, other.data);
// 5. 返回当前对象的引用
return *this;
}
// 辅助函数:显示字符串内容
void print() const {
std::cout << data << " (length: " << length << ")\n";
}
};
int main() {
MyString s1("Hello");
MyString s2("World");
std::cout << "Before assignment:\n";
s1.print(); // Hello (length: 5)
s2.print(); // World (length: 5)
s2 = s1; // 调用赋值运算符重载
std::cout << "\nAfter assignment:\n";
s1.print(); // Hello (length: 5)
s2.print(); // Hello (length: 5)
// 测试自赋值
s2 = s2;
s2.print(); // 应该仍然是Hello
return 0;
}
函数调用运算符重载
#include <iostream>
class MyAdd {
public:
int operator()(int a, int b) {
return a + b;
}
};
int main() {
// 创建函数对象实例
MyAdd adder;
// 使用函数对象
std::cout << adder(3, 4) << std::endl; // 输出 7
// 或者直接使用临时对象
std::cout << MyAdd()(3, 4) << std::endl; // 同样输出 7
return 0;
}
重载关系运算符
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
class Person {
std::string name;
int age;
double height;
public:
Person(const std::string& n, int a, double h)
: name(n), age(a), height(h) {}
// 手动实现比较运算符
// 等于运算符
bool operator==(const Person& other) const {
return name == other.name &&
age == other.age &&
std::abs(height - other.height) < 1e-5;
}
// 不等于运算符
bool operator!=(const Person& other) const {
return !(*this == other);
}
// 小于运算符
bool operator<(const Person& other) const {
if (age != other.age) return age < other.age;
if (std::abs(height - other.height) >= 1e-5)
return height < other.height;
return name < other.name;
}
// 其他运算符基于 < 和 == 实现
bool operator>(const Person& other) const { return other < *this; }
bool operator<=(const Person& other) const { return !(other < *this); }
bool operator>=(const Person& other) const { return !(*this < other); }
// 输出运算符
friend std::ostream& operator<<(std::ostream& os, const Person& p) {
os << p.name << " (" << p.age << ", " << p.height << "m)";
return os;
}
};
int main() {
Person alice("Alice", 30, 1.65);
Person bob("Bob", 25, 1.80);
Person alice2("Alice", 30, 1.65);
Person charlie("Charlie", 30, 1.70);
// 测试比较
std::cout << std::boolalpha;
std::cout << "alice == alice2: " << (alice == alice2) << "\n"; // true
std::cout << "alice != bob: " << (alice != bob) << "\n"; // true
std::cout << "alice < bob: " << (alice < bob) << "\n"; // false (30 > 25)
std::cout << "alice < charlie: " << (alice < charlie) << "\n"; // true (1.65 < 1.70)
// 在容器中使用
std::vector<Person> people = {bob, alice, charlie, alice2};
std::sort(people.begin(), people.end());
std::cout << "\n排序后:\n";
for (const auto& p : people) {
std::cout << p << "\n";
}
return 0;
}