C++运算符重载


前言

本篇文章主要介绍了C++运算符重载


1.运算符重载的概念

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

运算符重载的基本格式:
返回值类型 operator运算符 (参数列表)

大多数运算符可以被重载 不能重载的运算符只有5个:
运算符名称运算符
成员访问运算符.
成员指针访问运算符.*
域运算符::
长度运算符sizeof
条件运算符?:

2.运算符重载的两种方式

2.1 使用成员函数重载运算符(类内重载)

#include <iostream>
#include <string>

using namespace std;

/*
    实现A类,包含int类型变量的money
*/
class A{
public:
    A(int money = 0){
        this->money = money;
    }
    int getMoney(){
        return this->money;
    }
    // 重载A类的+号运算符
    A operator+(A &other){
        return A(this->money + other.money);
    }
private:
    int money;
};
int main() {
    A a1(100);
    A a2(200);
    A a3;    
    cout << "begin:" << a3.getMoney() << endl;
    a3 = a1 + a2;
    cout << "end:" << a3.getMoney() << endl;
    return  0;
}

2.2 使用友元函数重载运算符(类外重载)

#include <iostream>
#include <string>

using namespace std;

/*
    实现A类,包含int类型变量的money
*/
class A{
public:
    A(int money = 0){
        this->money = money;
    }
    int getMoney(){
        return this->money;
    }
    // 重载A类的+号运算符
    friend A operator+(const A a1,const A a2){
        return A(a1.money+a2.money);
    }
private:
    int money;
};
int main() {
    A a1(100);
    A a2(200);
    A a3;    
    cout << "begin:" << a3.getMoney() << endl; // output:0
    a3 = a1 + a2;
    cout << "end:" << a3.getMoney() << endl; //output:300
    return  0;
}

3.几种运算符重载

3.1 赋值运算符重载

#include <iostream>
#include <string.h>
#include <string>
#include <sstream>
using namespace std;

/*
    实现A类,包含int类型变量的money
*/
class A{
public:
    A(int money = 0,const char *name = ""){
        this->money = money;
        this->name = new char[strlen(name)+1];
        strcpy(this->name, name);
    }
    ~A(){
        if (this->name) {
            delete name;
        }
    }
    int getMoney(){
        return this->money;
    }
    string getInfo(){
        stringstream ret;
        ret << "name:" << name << "\tmoney:" << money;
        return ret.str();
    }
    //赋值运算符的重载:将money赋值给其他对象,注意浅拷贝还是深拷贝的问题
    A &operator=(const A &other){
        // if(name){
        //     delete name;
        // }
        // name = new char[strlen(other.name)+1];
        // strcpy(name, other.name);
        this->money = other.money; 
        return *this; //返回值,便于连续赋值 a1 = a2 = a3
    }  
    // 重载A类的+号运算符
    friend A operator+(const A &a1,const A &a2){
        return A(a1.money+a2.money);
    }

private:
    int money;
    char *name;
};
int main() {
    A a1(100,"a1");
    A a2(200,"a2");
    A a3(0,"a3");    
    cout << "begin:" << a3.getInfo() << endl; //output:a3 0
    a3 = a1 + a2;
    cout << "end:" << a3.getInfo() << endl;//output:a3 300
    return  0;
}

3.2 关系运算符的重载

	//关系运算符重载,返回值一般为bool类型
    bool operator>(const A &other){
        bool ret = false;
        if (this->money > other.money) {
            ret = true;
        }
        return ret;
    }

3.3 插入运算符(>>)和 提取运算符(<<)的重载

这里一般采用友元函数的形式重载(成员函数重载后写法是反的:a << cout)


    /*
	插入运算符和提取运算符的重载:
		cout 是 ostream类的对象。ostream 类和 cout 都是在头文件 <iostream>中声明的。ostream 类将<<重载为成员函数。
	*/
	// >> 重载,(如果要修改对象的内容不加const)
    friend istream &operator>>(istream &is, A &a1){
        is >> a1.money;
        return is;
    }
    // << 重载
    friend ostream &operator<<(ostream &os, const A &a1){
        os << a1.name << "\t" << a1.money;
        return os;
    }

//测试:
int main() {
    A a1(100,"a1");
    A a2(200,"a2");
    A a3(0,"a3");    
    a3 = a1 + a2;

    cout << a1 << endl << a2 << endl << a3 << endl;
    cin >> a1; //input:5000
    cout << a1; //output:a1 5000
    return  0;
}

3.4 前置运算符重载++ 和 后置运算符重载++

前置运算符,需要引用返回,不需要参数。返回自增后的值,且返回的是一个左值 ;
后置++,不需要引用返回,需要参数区分。返回自增前的值,且返回的是一个右值。

//前置++,++money
    A &operator++(){
        this->money++;
        return *this;
    }
    //后置++,money++
    const A operator++(int tmp){
        A a(this->money,this->name);
        this->money++;
        return a;
    }

//测试
int main() {
    A a1(100,"a1");
    A a2(200,"a2");
    A a3(0,"a3");    
    a3 = a1 + a2;

    cout << a3 << endl;
    cout << ++a3 << endl; //output:a3 301
    cout << a3++ << endl; //output:a3 301
    return  0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值