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

简介: 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;
}
相关文章
|
22天前
|
编译器 C++
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
26 1
|
27天前
|
程序员 编译器 C++
C++中的运算符重载(Operator Overloading)
C++中的运算符重载(Operator Overloading)
30 1
|
6天前
|
存储 Linux C语言
c++进阶篇——初窥多线程(二) 基于C语言实现的多线程编写
本文介绍了C++中使用C语言的pthread库实现多线程编程。`pthread_create`用于创建新线程,`pthread_self`返回当前线程ID。示例展示了如何创建线程并打印线程ID,强调了线程同步的重要性,如使用`sleep`防止主线程提前结束导致子线程未执行完。`pthread_exit`用于线程退出,`pthread_join`用来等待并回收子线程,`pthread_detach`则分离线程。文中还提到了线程取消功能,通过`pthread_cancel`实现。这些基本操作是理解和使用C/C++多线程的关键。
|
3天前
|
存储 编译器 C++
【C++】:拷贝构造函数和赋值运算符重载
【C++】:拷贝构造函数和赋值运算符重载
6 1
|
16天前
|
C++ 索引
C++核心技术要点《运算符重载》
C++核心技术要点《运算符重载》
23 2
|
3天前
|
API C++
c++进阶篇——初窥多线程(三)cpp中的线程类
C++11引入了`std::thread`,提供对并发编程的支持,简化多线程创建并增强可移植性。`std::thread`的构造函数包括默认构造、移动构造及模板构造(支持函数、lambda和对象)。`thread::get_id()`获取线程ID,`join()`确保线程执行完成,`detach()`使线程独立,`joinable()`检查线程状态,`operator=`仅支持移动赋值。`thread::hardware_concurrency()`返回CPU核心数,可用于高效线程分配。
|
1月前
|
编译器 C语言 C++
从C语言到C++⑤(第二章_类和对象_中篇)(6个默认成员函数+运算符重载+const成员)(下)
从C语言到C++⑤(第二章_类和对象_中篇)(6个默认成员函数+运算符重载+const成员)
12 1
|
1月前
|
程序员 C++
C++程序中的运算符重载
C++程序中的运算符重载
30 2
|
1月前
|
编译器 C++
C++|运算符重载(1)|为什么要进行运算符重载
C++|运算符重载(1)|为什么要进行运算符重载
|
1月前
|
编译器 C语言 C++
【C++从练气到飞升】05---运算符重载(二)
【C++从练气到飞升】05---运算符重载(二)