operator+ 应返回值而非引用,避免返回局部对象引用导致未定义行为;operator+= 才返回引用;输入输出流重载必须为非成员友元函数。
多数初学者在重载 operator+ 时习惯写成 MyClass& operator+(const MyClass& a, const MyClass& b),这会导致返回局部对象的引用,引发未定义行为。加法语义是生成新对象,不是修改原对象。
MyClass(值类型),让编译器决定是否启用 RVO 或移动构造operator+=
class Vec {
public:
int x, y;
Vec(int x = 0, int y = 0) : x(x), y(y) {}
Vec operator+(const Vec& other) const {
return Vec(x + other.x, y + other.y); // ✅ 返回值,安全
}
Vec& operator+=(const Vec& other) {
x += other.x; y += other.y;
return *this; // ✅ operator+= 才返回引用
}
};operator 和 operator>> 的左操作数是 std::ostream 或 std::istream,而你无法修改标准库类。所以它们只能是非成员函数;又因需访问类的私有成员,通常需加 friend 声明。
operator 写成 MyClass::operator —— 编译不过
friend,但违背封装原则std::ostream& operator
class Date {
int year, month, day;
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
friend std::ostream& operator<<(std::ostream& os, const Date& d) {
return os << d.year << "-" << d.month << "-" << d.day; // ✅ 可访问私有成员
}
};编译器靠参数列表区分前置(++a)和后置(a++)。C++ 规定后置版本必须接受一个 int(仅作标记,不使用),这是语法约定,不是设计选择。
T&(通常 *this 引用),无参数T(旧值副本),必须有一个 int 参数(哪怕没名字)const 限定符——如果你的类支持只读对象自增,前置也应加 const?不,因为要改状态,所以前置通常不加 const
class Counter {
int val;
public:
Counter(int v = 0) : val(v) {}
Counter& operator++() { // 前置
++val;
return *this;
}
Counter operator++(int) { // 后置:int 是必需的哑元
Counter old = *this;
++val;
return old;
}
};operator= 是唯一一个编译器默认生成、但用户重载后不会自动生成移动赋值的运算符。它必须处理 a = a 这种自我赋值,否则可能提前释放资源再试图访问野指针。
this == &other,但更推荐“拷贝-交换”惯用法(copy-and-swap),天然规避自我赋值和部分异常问题*this 引用,以支持链式赋值如 a = b
= c
class String {
char* data;
public:
String(const char* s = "") : data(new char[strlen(s)+1]) {
strcpy(data, s);
}
String& operator=(String other) { // 注意:传值,触发拷贝/移动
swap(data, other.data);
return *this; // ✅ 拷贝-交换,简洁且安全
}
void swap(char*& a, char*& b) { char* t = a; a = b; b = t; }
};C++ 运算符重载真正难的不是语法,而是对语义一致性的坚持——比如 operator== 必须满足自反、对称、传递,operator 要提供严格弱序。这些约束不会报错,但会在 std::sort 或 std::map 里悄悄出问题。