运算符重载(操作符重载):可以为运算符增加一些新的功能。
全局函数、成员函数都支持运算符重载。
#includeusing namespace std; class Point { friend Point operator+(const Point &, const Point &); int m_x; int m_y; public: Point(int x, int y) : m_x(x), m_y(y) {} void display() { cout << "(" << m_x << ", " << m_y << ")" << endl; } }; Point operator+(const Point &p1, const Point &p2) { return Point(p1.m_x + p2.m_x, p1.m_y + p2.m_y); } int main() { Point p1(10, 20); Point p2(20, 30); Point p3 = p1 + p2; p3.display(); Point p4 = p1 + p2 + p3; p4.display(); getchar(); return 0; }
#includeusing namespace std; class Point { friend ostream &operator<<(ostream &, const Point &); friend istream &operator>>(istream &, Point &); int m_x; int m_y; public: Point(int x, int y) : m_x(x), m_y(y) {} Point(const Point& point) { m_x = point.m_x; m_y = point.m_y; } void display() { cout << "(" << m_x << ", " << m_y << ")" << endl; } const Point operator+(const Point &point) const { return Point(m_x + point.m_x, m_y + point.m_y); } const Point operator-(const Point &point) const { return Point(m_x - point.m_x, m_y - point.m_y); } Point &operator+=(const Point &point) { m_x += point.m_x; m_y += point.m_y; return *this; } bool operator==(const Point &point) const { return (m_x == point.m_x) && (m_y == point.m_y); } bool operator!=(const Point &point) const { return (m_x != point.m_x) || (m_y != point.m_y); } const Point operator-() const { return Point(-m_x, -m_y); } // 前置++ Point &operator++() { m_x++; m_y++; return *this; } // 后置++ const Point operator++(int) { Point old(m_x, m_y); m_x++; m_y++; return old; } }; ostream &operator<<(ostream &cout, const Point &point) { cout << "(" << point.m_x << ", " << point.m_y << ")"; return cout; } istream &operator>>(istream &cin, Point &point) { cin >> point.m_x; cin >> point.m_y; return cin; } //class Person { //private: // int m_age; // int m_height; // void operator=(const Person &person) {} //public: // Person(int age = 0, int height = 0) :m_age(age), m_height(height) {} // // void display() { // cout << m_age << ", " << m_height << endl; // } //}; int main() { Point p1(10, 20); Point p2(20, 30); Point p3 = p1 + p2; cout << p3 << endl; cin >> p1; cout << p1 << endl; p3 += p1; cout << p3 << endl; getchar(); getchar(); return 0; }
#include调用父类的运算符重载函数using namespace std; class Person { private: int m_age; int m_height; void operator=(const Person &person) {} public: Person(int age = 0, int height = 0) : m_age(age), m_height(height) {} void display() { cout << m_age << ", " << m_height << endl; } }; int main() { Person p1(10, 180); Person p2(11, 175); p1 = p2; // 报错:=重载函数私有化 getchar(); return 0; }
#include仿函数using namespace std; class Person { int m_age; public: Person(int age) : m_age(age) {} Person &operator=(const Person &person) { m_age = person.m_age; return *this; } int getAge() { return m_age; } }; class Student : public Person { int m_score; public: Student(int age, int score) : m_score(score), Person(age) {} Student &operator=(const Student &student) { Person::operator=(student); m_score = student.m_score; return *this; } int getScore() { return m_score; } }; int main() { Student stu1(18, 90); Student stu2(20, 100); cout << stu2.getAge() << " " << stu2.getScore() << endl; stu2 = stu1; cout << stu2.getAge() << " " << stu2.getScore() << endl; getchar(); return 0; }
仿函数:将一个对象当作一个函数一样来使用。
对比普通函数,它作为对象可以保存状态。
#include注意using namespace std; class Sum { int m_age; public: int operator()(int a, int b) { return a + b; } }; int main() { Sum sum; cout << sum(10, 20) << endl; // 等价于sum.operator()(10, 20) getchar(); return 0; }
有些运算符不可以被重载,比如
对象成员访问运算符:.
域运算符:::
三目运算符:?:
sizeof
有些运算符只能重载为成员函数,比如:
赋值运算符 =
下标运算符 []
函数运算符 ()
指针访问成员 ->



