c++进阶篇(一)——运算符重载

什么是运算符重载

运算符重载:用同一个运算符完成不同的运算功能。
C++运算符重载的相关规定如下:
1.不能改变运算符的优先级。
2.不能改变运算符的结合性。
3.默认参数不能和重载的运算符一起使用,也就是说,在设计运算符重载成员函数时不能使用默认函数。
4.不能改变运算符的操作数的个数。
5.不能创建新的运算符,只有已有运算符可以被重载
6.运算符作用于C++内部提供的数据类型时,原来含义保持不变

相关代码

  • 关系运算符重载
#include <iostream>

using namespace std;

class Girl
{
private:
    int age;
    int figure;
    int yanzhi;
public:
    Girl(int a,int b,int c):age(a),figure(b),yanzhi(c)
    {}
    int Sum(const Girl& g1)
    {
        return age+figure+yanzhi;
    }
    bool operator < (const Girl& g1)
    {
        if(Sum(g1)<Sum(*this))
        {
            return true;
        }
        return false;
    }

    bool operator == (const Girl& g1)
    {
        if(Sum(g1)==Sum(*this))
        {
            return true;
        }
        return false;
    }

    bool operator >(const Girl& g1)
    {
        if(Sum(g1)>Sum(*this))
        {
            return true;
        }
        return false;
    }
};

int main()
{
    Girl g1(18,90,80),g2(17,80,80);
    if(g1<g2)
    {
        cout<<"g1 is better"<<endl;
    }
    if(g1==g2)
    {
        cout<<"g1 is equal to g2"<<endl;
    }
    if(g1>g2)
    {
        cout<<"g1 is better"<<endl;
    }
    return 0;
}
  • 左移运算符
    注意::左移运算符的重载函数不能作为成员函数,如果想调用类的私有成员需要使用友元函数
#include <iostream>

using namespace std;

class Girl
{
private:
    int age;
    string sex;
    string name;
public:
    Girl(int a,string b,string c):age(a),sex(b),name(c)
    {}
    friend ostream& operator <<(ostream& os,const Girl& g);//重载左移运算符
};

ostream& operator <<(ostream& os,const Girl& g)
{
    cout<<g.age<<g.sex<<g.name;
    return os;
} 


int main()
{
    Girl g(18,"女","小美");
    cout<<g<<endl;
    return 0;
}
  • 下标运算符
#include <iostream>
#include <vector>

using namespace std;

class girl
{
private:
    int age;
    string sex;
    string name;
    vector<string> boys;
public:
    girl(int a,string s,string n,vector<string> b)
    {
        age=a;
        sex=s;
        name=n;
        boys=b;
    }
    void operator[](int i)
    {
        if(i>boys.size()-1)
        {
            cout<<"error"<<endl;
            return;
        }
        cout<<boys[i]<<endl;
    }
};

int main()
{
    girl g(18,"girl","xiaohong",{"xiaoming","xiaohong"});
    g[0];
    g[1];
    g[2];//error
    return 0;
}
  • 赋值运算符
    注意: 系统自带的默认赋值函数是浅拷贝,如果类中成员有使用了堆区内存则需要自己重载赋值运算符
#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

class Girl
{
public:
    int age;
    string sex;
    string name;
    int* m_ptr;
    Girl()
    {
        m_ptr = nullptr;
    }
    //重载赋值运算符
    Girl& operator=(const Girl& g)
    {
        if(this==&g)
        {
            return *this;
        }
        if(g.m_ptr==nullptr)
        {
            if(m_ptr!=nullptr)
            {
                delete m_ptr;
                m_ptr = nullptr;
            }
        }
        else
        {
            if(m_ptr==nullptr)
            {
                m_ptr = new int;
            }
            memcpy(g.m_ptr, m_ptr, sizeof(int));
        }
        age=g.age;
        sex=g.sex;
        name=g.name;
        return *this;
    }

    void print()
    {
        cout<<"age:"<<age<<endl;
        cout<<"sex:"<<sex<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"*m_ptr:"<<*m_ptr<<endl;
        cout<<"m_ptr:"<<m_ptr<<endl;
        cout<<endl;
    }
};

int main()
{
    Girl g1;
    g1.age=18;
    g1.sex="girl";
    g1.name="xiaohong";
    g1.m_ptr = new int(100);
    g1.print();

    Girl g2;
    g2=g1;
    g2.print();
    return 0;
}
  • 內存分配符(new和delete)
    注意:这里虽然没有使用static关键字,但是这里对newdelete的重载函数属于类的静态成员函数,它并不能访问类的非静态成员。
#include <iostream>

using namespace std;

class girl
{
private:
    int age;
    int bh;

public:
    girl(int a, int b) : age(a), bh(b)
    {
        cout << "构造函数" << endl;
    }
    ~girl()
    {
        cout << "析构函数" << endl;
    }
    void *operator new(size_t size)
    {
        cout << "operator new" << endl;
        return malloc(size);
    }

    void operator delete(void *p)
    {
        cout << "operator delete" << endl;
        free(p);
    }
};

int main()
{
    girl *p = new girl(18, 170);
    delete p;
    return 0;
}
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落雨便归尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值