C++(十五) 运算符重载

简介: C++中的运算符重载允许对已有运算符的功能进行重新定义,从而扩展语言功能、简化代码并提升效率。重载遵循特定语法,如 `friend 类名 operator 运算符(参数)`。重载时需注意不可新增或改变运算符数量、语义、优先级、结合性和返回类型。常见示例包括双目运算符 `+=` 和单目运算符 `-` 及 `++`。输入输出流运算符 `<<` 和 `>>` 也可重载。部分运算符只能作为成员函数重载。

C++运算符重载

运算符重载的概念

运算符重载是一种在特定情况下,对已有运算符的功能进行重新定义的过程。

运算符重载的作用

运算符重载的作用主要有以下几点:

  1. 扩充语言的功能:运算符重载可以扩充语言的功能,使其能够支持新的运算符,并能够与其他运算符共同工作。

  2. 简化代码:运算符重载可以简化代码,使其更加易读、易写。

  3. 提高效率:运算符重载可以提高效率,因为它可以避免重复编写相同的代码。

运算符重载的语法

运算符重载的语法如下:

class 类名 {
public:
    // 重载运算符
    friend 类名 operator 运算符(类名 左侧操作数, 类名 右侧操作数);
};

其中,friend关键字用于声明重载的运算符函数为类的友元函数。

可被重载的运算符

img_7.png

运算符重载的注意事项

  1. 不能新增运算符
  2. 不能改变操作数的个数
  3. 不改变语义
  4. 不能改变运算符优先级
  5. 不能改变运算符的结合性
  6. 不能改变运算符的返回类型
  7. 重载运算符的函数不能有默认的参数

重载范例

双目例举

格式:

格式 L@R
全局函数 operator@(L,R)
成员函数 L.operator@(R)

operator+=

img_8.png

// todo 重载范例
#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(float x=0, float y=0)
            :_x(x),_y(y){}
    void dis()
    {
        cout<<"("<<_x<<","<<_y<<")"<<endl;
    }
    Complex& operator+=(const Complex &c)
    {
        this->_x += c._x;
        this->_y += c._y;
        return * this;
    }
private:
    float _x;
    float _y;
};

int main(){
    int a=10;
    int b=20;
    int c=30;
    a+=b;
    b+=c;
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<c<<endl;

    Complex c1(10,0),c2(20,0),c3(30,0);

    c1+=c2;
    c2+=c3;
    c1.dis();// (30,0)
    c2.dis();// (50,0)
    c3.dis();// (30,0)

    c1+=c2+=c3;

    c1.dis();//(110,0)
    c2.dis();// (80,0)
    c3.dis();// (30,0)

    (c1+=c2)+=c3;
    c1.dis();//(220,0)
    c2.dis();// (80,0)
    c3.dis();// (30,0)
}

单目例举

格式:

格式 @M 或 M@
全局函数 operator@(M)
成员函数 M.operator@( )

operator- (减号)

img_9.png

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(float x=0, float y=0)
            :_x(x),_y(y){}
    void dis()
    {
        cout<<"("<<_x<<","<<_y<<")"<<endl;
    }
    const Complex operator-() const {
        return Complex(-_x,-_y);
    }


private:
    float _x;
    float _y;
};

int main(){
    int a=10;
    //-a=20; //错误的写法
    cout<<-a<<endl;//-10
    cout<<a<<endl;//10

    cout<<-(-a)<<endl;//10 这种写法合法



    Complex c1(10,0);
    Complex cc=-c1;

    //-cc=Complex(20,0); // 错误的写法,但是  Complex operator-() 的时候不会报错  改成--> const Complex operator-() 保证与int一致性


    //-(-c1);// 这种写法不合法  此时的是--> const Complex operator-() 因为返回的是const 对象,const不能调用非const成员函数
    (c1.operator-()).operator-();//等价于 -(-c1)
    //--> const Complex operator-()  --> const Complex operator-() const  可以实现这种写法
    -(-c1);//不报错
    c1.dis(); // (10,0)
    cc.dis(); // (-10,0)

}

operator++()

++ 有前++ 和后++ 两种形式,前++ 先自增,后++ 先返回值,然后自增。

img_10.png

#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(float x=0, float y=0)
            :_x(x),_y(y){}
    void dis() const {
        cout<<"("<< this->_x<<","<< this->_y<<")"<<endl;
    }

    Complex & operator++()
    {
        cout<<"++a"<<endl;
        _x++;
        _y++;
        return *this;
    }

    const Complex  operator++(int)
    {
        cout<<"a++"<<endl;
        Complex temp(*this);
        _x++;
        _y++;
        return temp;
    }

    Complex & operator--()
    {
        cout<<"--a"<<endl;
        _x--;
        _y--;
        return *this;

    }


    const Complex  operator--(int)
    {
        cout<<"a--"<<endl;
        Complex temp(*this);
        _x--;
        _y--;
        return temp;
    }
private:
    float _x;
    float _y;
};

int main(){
    int a=10;

    cout<<++a<<endl;//11
    cout<<a<<endl;//11
    cout<<++++a<<endl;//11
    cout<<a<<endl;//11
    //cout<<a++++<<endl;//错误写法

    Complex c1(10,0);
    ++c1;
    c1.dis(); // (11,1)
    ++++c1;
    c1.dis(); // (13,3)

    Complex c2(20,0);
    c2++.dis();//(20,0)

    c2.dis(); // (21,1)

    //c2++++;//让这种写法不合法 Complex  operator++(int)加const

}

operator<</>>

operator>>:

基本数据 Complex x,y;
运算 cin>>x cin>>x>>y
返回参考 void istream&

operator<<:

基本数据 Complex x,y;
运算 cin<<x cin<<x<<y
返回参考 void ostream&
#include <iostream>
using namespace std;

class Complex
{
public:
    Complex(float x=0, float y=0):_x(x),_y(y){}

    friend istream&  operator >> (istream &in, Complex &c){
        in>>c._x>>c._y;
        //in>>c._y;
        return in;

    }
    friend ostream& operator << (ostream &out, Complex &c){
        out<<"["<<c._x<<" "<<c._y<<"]"<<endl;
        return out;
    }



private:
    float _x;
    float _y;
};

int main(){

    Complex c1(10,0);
    cout<<c1<<endl; // [10 0]

    cin>>c1;
    cout<<c1<<endl; // [10 0]

    return 0;
}

运算符重载小结

重载格式

img_11.png

不可重载运算符

img_13.png

只能重载为成员的运算符

img_14.png


img_15.png

相关文章
|
3月前
|
编译器 C++
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
37 1
|
4月前
|
程序员 编译器 C++
C++中的运算符重载(Operator Overloading)
C++中的运算符重载(Operator Overloading)
41 1
|
3月前
|
存储 编译器 C++
【C++】:拷贝构造函数和赋值运算符重载
【C++】:拷贝构造函数和赋值运算符重载
22 1
|
3月前
|
C++ 索引
C++核心技术要点《运算符重载》
C++核心技术要点《运算符重载》
46 2
|
2月前
|
自然语言处理 程序员 C++
C++基础知识(五:运算符重载)
运算符重载是C++中的一项强大特性,它允许程序员为自定义类型(如类或结构体)重新定义标准运算符的行为,使得这些运算符能够适用于自定义类型的操作。这样做可以增强代码的可读性和表达力,使得代码更接近自然语言,同时保持了面向对象编程的封装性。
|
2月前
|
Java 程序员 C++
|
2月前
|
编译器 C++
【C++】详解运算符重载,赋值运算符重载,++运算符重载
【C++】详解运算符重载,赋值运算符重载,++运算符重载
|
3月前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
3月前
|
C++
c++进阶篇(一)——运算符重载
c++进阶篇(一)——运算符重载
|
4月前
|
程序员 C++
C++程序中的运算符重载
C++程序中的运算符重载
35 2