运算符重载

简介: 运算符重载


运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

加号运算符重载

作用:实现两个自定义类型的相加的运算
如果现在我们有一个类如下:

class Person
{
    
public:
    Person()
    {
    
        m_a = 10;
        m_b = 30;
    }
public:
    int m_a;
    int m_b;
};

如果我们想实现

    Person p1;
    Person p2;
    Person p3 = p1 + p2;

按普通的加法肯定是不行,这1就要使用加号运算符重载
1.成员函数重载+号

class Person
{
    
public:
    Person()
    {
    
        m_a = 10;
        m_b = 30;
    }
    //成员函数重载 +
    Person operator+(Person& p)
    {
    
        Person temp;
        temp.m_a = this->m_a + p.m_a;
        temp.m_b = this->m_b + p.m_b;
        return temp;
    }

public:
    int m_a;
    int m_b;
}

本质:

      Person p3 = p1 + p2;
本质:Person p3 = p1.operator+(p2);

2.全局函数重载

Person operator+(Person& p1, Person& p2)
{
    
    Person temp;
    temp.m_a = p1.m_a + p2.m_a;
    temp.m_b = p1.m_b + p2.m_b;
    return temp;
}

本质:

      Person p3 = p1 + p2;
本质:Person p3 = operator+(p1, p2);

运算符重载函数也是可以发生重载的

左移运算符重载

作用:输出自定义类型
比如:
先准备一个类

#include
using namespace std;
class Person
{
    
public:
    Person()
    {
    
        m_a = 10;
        m_b = 10;
    }
    int m_a;
    int m_b;
};

如果我们想实现

Person p;
cout<<p<<endl;

我们就需要利用左移运算符重载

利用全局函数重载左移运算符

ostream& operator<<(ostream &cout, Person p)
{
    
    cout << "m_a= " << p.m_a << "   m_b = " << p.m_b;
    return cout;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

using namespace std;
class Person
{
    
public:
    Person()
    {
    
        m_Num = 0;
    }
    int m_Num;
};

假如我们要实现:

Person p;
p++;
++p;

我们就需要用到递增运算符重载

前置++

using namespace std;
class Person
{
    
public:
    Person()
    {
    
        m_Num = 0;
    }
    //重载前置++运算符  返回引用为了一直对一个数据进行递增操作
    Person& operator++()
    {
    
        m_Num++;
        return *this;
    }
    int m_Num;
};

后置++:

class Person
{
    
    friend ostream& operator<<(ostream& cout, Person p);
public:
    Person()
    {
    
        m_Num = 0;
    }
    //重载后置++运算符   用占位参数来区分后置++
    Person operator++(int)
    {
    
        Person  temp = *this;
        m_Num++;
        return temp;
    }
    int m_Num;
};

赋值运算符重载

C++编译器会有默认的赋值运算符重载,
赋值运算符operator=,对属性进行值拷贝
但这个默认的赋值运算符operator=,只能对属性进行值拷贝也就是浅拷贝,
如果要进行深拷贝的话,就需要自己写这个赋值运算符重载

#include
using namespace std;
class Person
{
    
public:
    Person(int a)
    {
    
        age = new int(a);
    }
    Person& operator=(Person& p)  //赋值运算符重载
    {
    
        if (age != NULL)
        {
    
            delete age;
            age = NULL;
        }
        age = new int(*p.age);
        return *this;  //返回这个时赋值时可以进行连等操作
    }
    ~Person()    //析构函数,在程序结束后释放空间
    {
    
        if (age != NULL)
        {
    
            delete age;
            age = NULL;
        }
    }
public:
    int* age;

};

关系运算符重载

作用:重载关系运算符,可以让两个自定义类型的对象进行对比操作
比如:
比较两个类的大小关系
要实现这个就需要重载关系运算符

#include
#include
using namespace std;
class Person
{
    
public:
    bool operator==(Person& p)
    {
    
        if (this->age == p.age && this->name == p.name)
            return true;
        else
            return false;
    }
    bool operator!=(Person& p)
    {
    
        if (this->age != p.age || this->name == p.name)
            return true;
        else
            return false;
    }
public:
    string name;
    int age;
};

这样我们就可以进行下面操作了

int main()
{
    
    Person p1;
    p1.age = 18;
    p1.name = "abd";
    Person p2;
    p2.age = 18;
    p2.name = "abd";
    if (p1 == p2)
        cout << "yes" << endl;
    if (p1 != p2)
        cout << "yes" << endl;
}

函数调用运算符重载

函数调用运算符()也可以进行重载
由于重载后的使用方法非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活。

如果我们要写一个打印类

#include
#include
class Mprint
{
    
public:
    void operator()(string s)
    {
    
        cout << s << endl;
    }
};
int main()
{
    
    Mprint mprintf;
    mprintf("hello word");   //由于使用方法特别像函数调用,所以被称为仿函数
}

如果想实现一个加法类:

#include
#include
class MyAdd
{
    
public:
    int operator()(int a, int b)
    {
    
        return a + b;
    }
};
int main()
{
    
    MyAdd add;
    int ret = add(20, 30);
    cout << ret << endl;
    //匿名对象调用
    cout << MyAdd()(20, 30) << endl;
}
相关文章
|
4月前
|
C++
|
编译器 程序员 C++
C++的运算符重载介绍
所谓重载,就是赋予新的含义。函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作。运算符重载(Operator Overloading)也是一个道理,同一个运算符可以有不同的功能。 实际上,我们已经在不知不觉中使用了运算符重载。例如,+号可以对不同类型(int、float 等)的数据进行加法操作;<<既是位移运算符,又可以配合 cout 向控制台输出数据。 C++ 本身已经对这些运算符进行了重载。C++ 也允许程序员自己重载运算符,这给我们带来了很大的便利。 下面的代码定义了一个复数类,通过运算符重载,可以用+号实现复数的加法运算:
62 0
|
4月前
|
程序员 C++
C++程序中的运算符重载
C++程序中的运算符重载
35 2
|
4月前
|
C++
C++运算符重载
C++运算符重载
32 0
|
4月前
|
C++
【C++】——运算符重载
【C++】——运算符重载
|
4月前
|
算法 C++ 开发者
40运算符重载
40运算符重载
20 0
|
4月前
|
C++
【C++】运算符重载
【C++】运算符重载
23 0
|
算法 安全 程序员
【为什么】C++ 中需要运算符重载
【为什么】C++ 中需要运算符重载
|
编译器 C++
第五层:C++中的运算符重载(下)
第五层:C++中的运算符重载(下)
第五层:C++中的运算符重载(下)
|
编译器 程序员 C++
第五层:C++中的运算符重载(上)
第五层:C++中的运算符重载(上)
第五层:C++中的运算符重载(上)