c++运算符重载怎么写 c++ operator重载规则【教程】

运算符重载允许自定义类型使用内置运算符;成员函数适用于左操作数为本类对象(如+、==、[]、=、->、()),非成员函数(常为友元)支持对称操作(如int+obj)和流操作(

运算符重载是 C++ 中让自定义类型像内置类型一样使用 +、==、[]、

哪些运算符能重载?怎么选成员 or 全局?

大部分运算符都可重载,包括算术(+ - * / %)、关系(== != =)、逻辑(&& || !)、赋值(= += -=)、下标([])、函数调用(())、指针访问(->)、输入输出(>)、new/delete 等。

选择成员函数还是非成员函数,主要看左操作数是否必须是当前类对象:

  • 成员函数:适合左操作数是本类对象的情形(如 a + b,a 是类对象),自动获得 this 指针,只需声明一个参数(右操作数)。赋值 =、下标 []、函数调用 ()、箭头 -> 必须是成员函数。
  • 非成员函数(常为友元):适合需要左操作数为其他类型(比如 int + obj),或希望对称支持(obj + 1 和 1 + obj 都成立)。此时需两个参数,常声明为类的 friend 才能访问私有成员。

常见运算符重载写法示例

假设有一个简单的 Vector2 类:

class Vector2 {
public:
    float x, y;
    Vector2(float x = 0, float y = 0) : x(x), y(y) {}
// 成员版:+(左操作数是 Vector2)
Vector2 operator+(const Vector2& other) const {
    return Vector2(x + other.x, y + other.y);
}

// 成员版:+=(修改自身,返回引用)
Vector2& operator+=(const Vector2& other) {
    x += other.x; y += other.y;
    return *this;
}

// 成员版:==(常量成员,不修改对象)
bool operator==(const Vector2& other) const {
    return x == other.x && y == other.y;
}

// 成员版:[](非常量和常量两个版本)
float& operator[](int i) {
    return (i == 0) ? x : y;
}
const float& operator[](int i) const {
    return (i == 0) ? x : y;
}

};

// 非成员版:stream) std::ostream& operator

// 非成员版:+ 支持 int + Vector2(需友元或 public 成员) Vector2 operator+(float s, const Vector2& v) { return Vector2(s + v.x, s + v.y); }

必须注意的细节和陷阱

写错容易引发隐式转换、效率问题或语义混乱:

  • 返回类型要合理:+ 应返回新对象(值语义),+= 应返回 *this 的引用;> 必须返回流引用以便链式调用(cout
  • const 正确性:不修改对象的运算符(如 +、==、[] const 版)必须加 const;参数也尽量用 const 引用避免拷贝。
  • 避免隐式转换干扰:如果类有单参数构造函数(如 Vector2(float)),编译器可能自动转换 int → Vector2,导致 1 + v 意外调用 + 成员函数。加 explicit 可禁止该转换。
  • 对称性考虑:若希望 a + 1 和 1 + a 都有效,只写成员版不够,必须补充非成员版(且不能依赖隐式转换,否则 1 + a 可能调两次转换)。

输入输出运算符 > 的特殊要求

它们必须是非成员函数,因为左操作数是 std::istream 或 std::ostream,你无法给标准库类添加成员函数。同时,通常需要访问类的私有成员,所以常声明为 friend:

class Vector2 {
    float x, y;
public:
    friend std::ostream& operator<<(std::ostream& os, const Vector2& v);
    friend std::istream& operator>>(std::istream& is, Vector2& v);
};

std::ostream& operator<<(std::ostream& os, const Vector2& v) { os << v.x << " " << v.y; // 直接访问私有成员 return os; }

std::istream& operator>>(std::istream& is, Vector2& v) { is >> v.x >> v.y; return is; }