C++之双目运算符重载

文章目录

运算符重载

cpp 复制代码
#include <iostream>
using namespace std;
class Complex{
private:
    double r;
    double i;
public:
    Complex(double r, double i){
        this->r = r;
        this->i = i;
    }
    void print(void){
        cout << r << " + " << i << "i" << endl;
    }
};
int main(void){
    Complex a(1,2);
    Complex b(3,4);
    a.print();
    b.print();
    int x = 10;
    int y = 20;
    int z = x + y;
    a+b; //想像x+y 实现Complex对象加运算 +运算符新的逻辑功能 要实现+运算符的重载
    return 0;
}

双目运算符重载

  • 双目运算符:有左右两个操作数的操作符 L#R
    • 算术运算:*、/、%、+、-
    • 关系运算:>、>=、<、<=、==、!=
    • 逻辑运算:&&、||
    • 位运算:&、|、^、<< 、>>
    • 赋值与复合赋值:=、+=、-=、*=、/=、%=、&=、|=、^=、<<= 、>>=
    • ... ...
  • 注意:
    • 表达式结果是右值
    • 左右操作数既可以是左值也可以是右值
  • 实现方式:
    • 成员函数形式:L.operator#®
    • 友元函数形式:operator#(L,R)
cpp 复制代码
#include <iostream>
using namespace std;
class Complex{
private:
    double r;
    double i;
public:
    Complex(double r, double i){
        this->r = r;
        this->i = i;
    }
    void print(void){
        cout << r << " + " << i << "i" << endl;
    }
    const Complex operator+(const Complex& c){
        Complex tmp(r+c.r, i+c.i);
        return tmp;
    }
    friend const Complex operator-(const Complex& l , const Complex& r);
};
const Complex operator-(const Complex& l, const Complex& r){
    Complex tmp(l.r - r.r, l.i - r.i);
    return tmp;
}
int main(void){
    Complex a(1,2);
    Complex b(3,4);
    a.print();
    b.print();
    Complex c = a+b;// a.operator+(b);
    c.print();
    Complex d = c - a; // operator-(c, a);
    d.print();
    return 0;
}
  • 对赋值类双目运算符重载时需要注意的事项:
    • 表达式的结果是左值,就是左操作数的自身
    • 左操作数必须是左值,右操作数可以是左值也可以是右值
cpp 复制代码
#include <iostream>
using namespace std;
class Complex{
private:
    double r;
    double i;
public:
    Complex(double r, double i){
        this->r = r;
        this->i = i;
    }
    void print(void){
        cout << r << " + " << i << "i" << endl;
    }
    const Complex operator+(const Complex& c){
        Complex tmp(r+c.r, i+c.i);
        return tmp;
    }
    Complex & operator+=(const Complex &c){
        r = r + c.r;
        i = i + c.i;
        return *this;
    }
    friend const Complex operator-(const Complex& l , const Complex& r);
    friend Complex & operator-=(Complex &L, const Complex &R);
};
const Complex operator-(const Complex& l, const Complex& r){
    Complex tmp(l.r - r.r, l.i - r.i);
    return tmp;
}
Complex & operator-=(Complex &L, const Complex &R){
    L.r -= R.r;
    L.i -= R.i;
    return L;
}
int main(void){
    Complex a(1,2);
    Complex b(3,4);
    a.print();
    b.print();
    Complex c = a+b;// a.operator+(b);
    c.print();
    Complex d = c - a; // operator-(c, a);
    d.print();
    a += b; //a.operator+=(b);
    a.print();
    (a -= b).print();
    return 0;
}
相关推荐
t***5441 天前
如何在现代C++中更有效地应用这些模式
java·开发语言·c++
itman3011 天前
C语言、C++与C#深度研究:从底层到现代开发演进全解析
c语言·c++·c·内存管理·编译模型
Hical_W1 天前
为 C++ Web 框架设计三层 PMR 内存池:从原理到实战
c++·github
BestOrNothing_20151 天前
C++零基础到工程实战(3.6):逻辑实战示例—日志模块
c++·命令行参数·main函数·switch case·逻辑判断·if else·enum class
t***5441 天前
有哪些常见的架构设计模式在现代C++中应用
开发语言·c++
zopple1 天前
汇编、C、C++和Java核心技术对比
c语言·汇编·c++
汉克老师1 天前
GESP2024年3月认证C++三级( 第三部分编程题(1、字母求和)
c++·string·gesp三级·gesp3级·大小写判断
沐雪轻挽萤1 天前
10. C++17新特性-保证的拷贝消除 (Guaranteed Copy Elision / RVO)
开发语言·c++
leaves falling1 天前
C/C++ 的内存管理,函数栈帧详讲
java·c语言·c++
wuyoula1 天前
AI导航智能决策系统源码 附教程
c++·tcp/ip·源码