运算符重载

    科技2024-08-11  29

    目录

    运算符重载重载操作符的两种形式1. 类的成员函数2. 类的友元函数复数Point类调用操作符重载operator()下标操作符重载operator []

    运算符重载

    不能重载的运算符(4个带点的+1个sizeof)

    ..*::?:sizeof

    重载操作符的两种形式

    1. 类的成员函数

    成员函数有一个隐含的this形参,限定其为第一个操作数。作为成员重载的操作符,要求访问重载函数的第一个操作数必须为该类的对象。

    //+ Complex c3 = c1 + c2;==》Complex c3 = c1.operator+(c2); ++i;//左值,对象 & operator ++ (){ //++ return *this; } i++;//右值,值 operator ++ (int)

    必须定义为类的成员函数:赋值= 下标[ ] 调用( ) 成员访问-> 。 优先定义为成员函数: 1.复合的赋值操作符(+= -= *= /=等),因为 返回的是左操作数本身。 2.改变对象的状态或与给定类型紧密联系的操作符,如自增++,自减–,解引用*等单目运算符,

    2. 类的友元函数

    必须定义成类的友元函数:输入操作符>>,输出操作符<< 优先定义成类的友元函数:对称的操作符,如算术操作符±*/,关系操作符><==(关系操作符重载函数必须返回true(非0)或false(0),即bool类型的值。),位操作符&|~^,双目运算符?:等。

    //<<输出操作符 Complex c1; cout<<c1<<endl;===operator <<(cout,c1);

    复数

    #include <iostream> using namespace std; class Complex{ private: int real; int imaginary; public: Complex(int real = 0, int imaginary = 0):real(real), imaginary(imaginary){} friend Complex operator+(const Complex &lc, const Complex &rc); friend Complex operator-(const Complex &lc, const Complex &rc); friend bool operator==(const Complex &lc, const Complex &rc); //i++ Complex operator ++(int){ Complex result; result = *this; this->real++; this->imaginary++; return result; } //++i Complex& operator ++(){ ++this->real; ++this->imaginary; return *this; } //i-- Complex operator --(int){ Complex result; result = *this; this->real--; this->imaginary--; return result; } //--i Complex& operator --(){ --this->real; --this->imaginary; return *this; } void print(){ cout << real << "+" << imaginary << "i" << endl; } }; Complex operator + (const Complex& lc, const Complex &rc){ Complex result; result.real = lc.real + rc.real; result.imaginary = lc.imaginary + rc.imaginary; return result; } Complex operator - (const Complex& lc, const Complex &rc){ Complex result; result.real = lc.real - rc.real; result.imaginary = lc.imaginary - rc.imaginary; return result; } bool operator==(const Complex &lc, const Complex &rc){ if (lc.real == rc.real && lc.imaginary == rc.imaginary){ return true; } return false; } void testPlus(){ cout<< "====testPlus===="<<endl; Complex a(1, 1); Complex b(2, 2); Complex c; c = a + b; c.print(); } void testPlusPlus(){ cout<< "====testPlusPlus===="<<endl; Complex c(0, 0); Complex d; d = c++; d.print(); c.print(); } void PlusPlustest(){ cout<< "====PlusPlustest===="<<endl; Complex c(0, 0); Complex d; d = ++c; d.print(); c.print(); } void test(){ cout<< "====test===="<<endl; int i = 0; int j; j = ++(i) ; cout << j << endl; cout << i << endl; } void testEqualEqual(){ cout<< "====testEqualEqual===="<<endl; Complex a(1, 2); Complex b(1, 2); Complex c(1, 3); if (a == b){ cout << "a == b" << endl; } if (a == c){ cout << "a == c" << endl; } else{ cout << "a != c" << endl; } } int main(void){ testPlus(); testPlusPlus(); PlusPlustest(); test(); testEqualEqual(); return 0; }

    Point类

    #include <iostream> using namespace std; class Point{ private: double x; double y; public: Point(double x = 0, double y = 0) :x(x), y(y){} Point(Point &other) :x(other.x),y(other.y){} double getx(){ return x; } double gety(){ return y; } void setx(double x){ this->x = x; return ; } void sety(double y){ this->y = y; return ; } void print(){ cout << "(" << x << "," << y << ")" << endl; return ; } Point& operator = (const Point &r){ if (this == &r){ return *this; } this->x = r.x; this->y = r.y; return *this;//为了连续赋值 } //++在前 Point& operator ++ (){ this->x = this->x + 1; this->y = this->y + 1; return *this; } //在后++ Point operator ++ (int){ Point temp = *this; this->x = this->x + 1; this->y = this->y + 1; return temp; } Point& operator -- (){ this->x = this->x - 1; this->y = this->y - 1; return *this; } Point operator -- (int){ Point temp = *this; this->x = this->x - 1; this->y = this->y - 1; return temp; } Point& operator += (const Point &r){ this->x += r.x; this->y += r.y; return *this; } friend Point operator + (Point &l, Point &r); friend Point operator - (Point &l, Point &r); friend Point operator * (Point &l, Point &r); friend Point operator / (Point &l, Point &r); friend bool operator > (Point &l, Point &r); friend bool operator < (Point &l, Point &r); friend bool operator == (Point &l, Point &r); friend ostream &operator<<(ostream &out, const Point &point); friend istream &operator>>(istream &in, Point &point); }; Point operator + (Point &l, Point &r){ Point result; result.x = l.x + r.x; result.y = l.y + r.y; return result; } Point operator - (Point &l, Point &r){ Point result; result.x = l.x - r.x; result.y = l.y - r.y; return result; } Point operator * (Point &l, Point &r){ Point result; result.x = l.x * r.x; result.y = l.y * r.y; return result; } Point operator / (Point &l, Point &r){ Point result; result.x = l.x / r.x; result.y = l.y / r.y; return result; } bool operator > (Point &l, Point &r){ if (l.x > r.x && l.y > r.y){ return true; } return false; } bool operator < (Point &l, Point &r){ if (l.x < r.x && l.y < r.y){ return true; } return false; } bool operator == (Point &l, Point &r){ if (l.x == r.x && l.y == r.y){ return true; } return false; } //输出操作符重载 ostream& operator << (ostream& out, const Point& point){ out << "(" << point.x << "," << point.y << ")"; return out; } //输入操作符重载 istream& operator >> (istream& in, Point& point){ in >> point.x >> point.y; return in; } void testOut(){ cout << "===testOut===" << endl; Point p1(1, 2); Point p2(3, 5); cout << p1 << "," << p2 << endl; } void testIn(){ cout << "===testIn===" << endl; Point p1; cin >> p1; cout << p1 << endl; } int main(void){ Point a(0, 1); Point b(1, 1); Point c; c = a + b; c.print(); c = a - b; c.print(); if (a == b) { cout << "a = b" << endl; }else{ cout << "!" << endl; } (c = a) = b; c.print(); a.print(); b.print(); a++; a.print(); testOut(); testIn(); return 0; }

    调用操作符重载operator()

    必须定义为类的成员函数,可以重载;定义了调用操作符的类,其类对象称为函数对象,常用于标准库的算法

    #include <iostream> using namespace std; class Abs{ public: Abs(){} int operator() (int i){ if (i < 0){ i = -i; } return i; } double operator() (double d){ if (d < 0){ d = -d; } return d; } }; int main(void){ Abs a; cout << a(1)<< endl; cout << a(-1)<< endl; cout << a(1.1)<< endl; cout << a(-1.1)<< endl; return 0; }

    下标操作符重载operator []

    可以从容器中检索单个元素的容器类一般会定义下标操作符重载。下标操作符必须定义为成员函数。要使其在用作左值和右值的时候都正常(返回对象引用)。下标操作符会定义两个版本,一个为非const成员并返回引用,一个为const成员并返回const引用。 // 下标运算符重载 char &operator[] (const unsigned int index){ if (m_str == NULL) { cout << "空字符串" << endl; return '\0'; } if (index > strlen(m_str)){ cout << "越界" << endl; return '\0'; } return *(m_str + index); } //应对 const Mystring str("hello");//const 对象只能调用const函数 cout << str[1] << endl; const char &operator[] (const unsigned int index) const{ if (m_str == NULL) { cout << "空字符串" << endl; return '\0'; } if (index > strlen(m_str)){ cout << "越界" << endl; return '\0'; } return *(m_str + index); }
    Processed: 0.011, SQL: 8