运算符重载 我们使用operator来重载运算符。
什么是运算符重载 运算符重载就是让结构体也能使用普通的运算符,比如 +, -, *, == 等等。
基本格式 1 2 3 返回值类型 operator 运算符(参数) { }
一元运算符重载 正号和负号运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 struct Point { int x, y; Point operator +() const { return Point (x, y); } Point operator -() const { return Point (-x, -y); } }; Point p (3 , 4 ) ;Point pos = +p; Point neg = -p;
自增运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 struct Counter { int value; Counter (int v = 0 ) : value (v) {} Counter& operator ++() { this ->value++; return *this ; } Counter operator ++(int ) { Counter old = *this ; this ->value++; return old; } }; Counter c (10 ) ;++c; Counter d = c++;
二元运算符重载 算术运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 struct Point { int x, y; Point (int x = 0 , int y = 0 ) : x (x), y (y) {} Point operator +(const Point& other) const { return Point (this ->x + other.x, this ->y + other.y); } Point operator -(const Point& other) const { return Point (this ->x - other.x, this ->y - other.y); } Point operator *(int scalar) const { return Point (this ->x * scalar, this ->y * scalar); } Point operator /(int scalar) const { return Point (this ->x / scalar, this ->y / scalar); } }; Point a (1 , 2 ) , b (3 , 4 ) ;Point c = a + b; Point d = a - b; Point e = a * 5 ; Point f = a / 2 ;
比较运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 struct Point { int x, y; Point (int x = 0 , int y = 0 ) : x (x), y (y) {} bool operator ==(const Point& other) const { return this ->x == other.x && this ->y == other.y; } bool operator !=(const Point& other) const { return !(*this == other); } bool operator <(const Point& other) const { if (this ->x != other.x) { return this ->x < other.x; } return this ->y < other.y; } bool operator >(const Point& other) const { return other < *this ; } }; Point a (1 , 2 ) , b (1 , 2 ) , c (2 , 3 ) ;bool eq = (a == b); bool neq = (a != b); bool less = (a < c);
赋值运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 struct Point { int x, y; Point (int x = 0 , int y = 0 ) : x (x), y (y) {} Point& operator =(const Point& other) { if (this != &other) { this ->x = other.x; this ->y = other.y; } return *this ; } Point& operator +=(const Point& other) { this ->x += other.x; this ->y += other.y; return *this ; } Point& operator -=(const Point& other) { this ->x -= other.x; this ->y -= other.y; return *this ; } }; Point a (1 , 2 ) , b (3 , 4 ) ;a = b; a += b;
特殊运算符重载 下标运算符 [] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 struct Array { int data[100 ]; int size; Array () : size (0 ) { for (int i = 0 ; i < 100 ; i++) data[i] = 0 ; } int & operator [](int index) { return this ->data[index]; } int operator [](int index) const { return this ->data[index]; } }; Array arr; arr[0 ] = 10 ; int val = arr[0 ];
函数调用运算符 () 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 struct Adder { int base; Adder (int b = 0 ) : base (b) {} int operator () (int value) const { return this ->base + value; } int operator () (int a, int b) const { return this ->base + a + b; } }; Adder add5 (5 ) ;int result1 = add5 (10 ); int result2 = add5 (10 , 20 );
箭头运算符 -> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 struct SmartPointer { int * ptr; SmartPointer (int * p = nullptr ) : ptr (p) {} int * operator ->() { return this ->ptr; } int & operator *() { return *(this ->ptr); } ~SmartPointer () { delete ptr; } }; int * raw_ptr = new int (42 );SmartPointer smart_ptr (raw_ptr) ;cout << *smart_ptr << endl; cout << smart_ptr->operator int () << endl ;
类型转换运算符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 struct Point { int x, y; Point (int x = 0 , int y = 0 ) : x (x), y (y) {} operator int () const { return this ->x + this ->y; } operator double () const { return sqrt (this ->x * this ->x + this ->y * this ->y); } }; Point p (3 , 4 ) ;int sum = p; double dist = p;
完整示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 #include <bits/stdc++.h> using namespace std;struct Complex { double real, imag; Complex (double r = 0 , double i = 0 ) : real (r), imag (i) {} Complex operator +(const Complex& other) const { return Complex (this ->real + other.real, this ->imag + other.imag); } Complex operator *(const Complex& other) const { return Complex ( this ->real * other.real - this ->imag * other.imag, this ->real * other.imag + this ->imag * other.real ); } bool operator ==(const Complex& other) const { return this ->real == other.real && this ->imag == other.imag; } Complex operator -() const { return Complex (-this ->real, -this ->imag); } Complex& operator =(const Complex& other) { if (this != &other) { this ->real = other.real; this ->imag = other.imag; } return *this ; } operator double () const { return sqrt (this ->real * this ->real + this ->imag * this ->imag); } void print () const { cout << this ->real << "+" << this ->imag << "i" << endl; } }; int main () { Complex a (3 , 4 ) , b (1 , 2 ) ; Complex c = a + b; c.print (); Complex d = a * b; d.print (); Complex e = -a; e.print (); double mag = a; cout << "Magnitude: " << mag << endl; return 0 ; }
一些话 这里使用了 AI 生成,因为运算符重载是在是太太太太太太太多了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!