当前位置: 首页 > news >正文

C++——运算符重载

1、运算符重载的概念

  1. 运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
  2. 运算符重载的目的是让语法更加简洁
  3. 运算符重载不能改变本来寓意,不能改变基础类型寓意
  4. 运算符重载的本质是另一种函数调用(是编译器去调用)
  5. 这个函数统一的名字叫operator
  6. 重载函数可以写成全局或成员函数
  7. 重载函数如果写成全局的,那么双目运算符左边的是第一个参数,右边是第二个参数
  8. 重载函数如果写成成员函数,那么双目运算符的左边是this,右边是第一个参数
  9. 不能改变运算符优先级,不能改变运算符的参数个数。

2、加号运算符重载

1、同类型的对象相加

#include <iostream>
using namespace std;class Maker
{
public:Maker(int id, int age){this->id = id;this->age = age;}//写成成员函数,那么只需要一个参数,这个参数是加号的右边Maker operator+(Maker& m2){Maker temp(this->id + m2.id, this->age + m2.age);return temp;}
public:int id;int age;
};
//重载加号运算符  全局函数方式
//Maker operator+(Maker &p1,Maker &p2)
//{
//	Maker temp(p1.id + p2.id, p1.age + p2.age);
//	return temp;
//}
void test()
{Maker m1(1, 10);Maker m2(2, 20);Maker m3 = m1 + m2;cout << m3.id << endl;cout << m3.age << endl;
}
int main()
{test();return 0;
}

2、不同对象类型相加

#include <iostream>
using namespace std;class Maker
{
public:Maker(int id, int age){this->id = id;this->age = age;}//写成成员函数,那么只需要一个参数,这个参数是加号的右边Maker operator+(Maker& m2){Maker temp(this->id + m2.id, this->age + m2.age);return temp;}
public:int id;int age;
};class Student
{
public:Student() {mid = 0;}Student(int id) {mid = id;}
public:int mid;
};Maker operator+(Maker& m1, Student& s1)
{Maker temp(m1.id + s1.mid, 20);return temp;
}
Student operator+(Student& s1, Maker& m1)
{Student temp(s1.mid + m1.id);return temp;
}
void test()
{Maker m1(1, 10);Student s1(2);Maker m3 = m1 + s1;cout << m3.id << endl;Student s2 = s1 + m1;cout << s2.mid<<endl;
}
int main()
{test();return 0;
}

3、减号运算符重载

#include <iostream>
using namespace std;class Maker
{
public:Maker(int id, int age){this->id = id;this->age = age;}//写成成员函数,那么只需要一个参数,这个参数是加号的右边Maker operator-(Maker& m2){Maker temp(this->id - m2.id, this->age - m2.age);return temp;}
public:int id;int age;
};
void test()
{Maker m1(10, 18);Maker m2(5, 15);Maker m3 = m1 - m2;cout << m3.id << endl;cout << m3.age << endl;
}
int main()
{test();return 0;
}

4、左移运算符重载

#include <iostream>
#include <iostream>
using namespace std;class Maker
{
public:Maker(int id, string name){this->id = id;this->name = name;}
public:int id;string name;
};
//1、形参和实参是一个对象
//2、不能改变库类中的代码
//3、ostream中把拷贝构造函数私有化了
//4、如果要和endl一起使用,那么必须返回ostream的对象
ostream& operator<<(ostream& out, Maker& m1)
{cout << m1.id << " " << m1.name << endl;return out;
}
void test()
{Maker m1(10, "薯片");cout << m1 << endl;
}
int main()
{test();return 0;
}

5、右移运算符

#include <iostream>
#include <iostream>
using namespace std;class Maker
{
public:Maker(int id, string name){this->id = id;this->name = name;}int getAge() {return this->id;}
public:int id;string name;
};istream &operator>>(istream& in, Maker& m1)
{cin >> m1.id;cin >> m1.name;return in;
}
void test()
{Maker m(10, "薯片");cin >> m;cout << m.getAge() << endl;
}
int main()
{test();return 0;
}

6、关系运算符重载

#include <iostream>
#include <iostream>
using namespace std;class Maker
{
public:Maker() {};Maker(int id){this->id = id;}bool operator==(Maker &m) {if (this->id == m.id) {return true;}return false;}bool operator!=(Maker& m) {if (this->id != m.id) {return true;}return false;}
public:int id;
};void test()
{Maker m1(10);Maker m;if (m1 == m) {cout << "真" << endl;}else {cout << "假" << endl;}if (m1 != m) {cout << "真" << endl;}else {cout << "假" << endl;}}
int main()
{test();return 0;
}

7、前置加加和后置加加

#include <iostream>
#include <iostream>
using namespace std;class Maker
{friend ostream& operator<<(ostream& os, Maker& m);
public:Maker(int id){this->id = id;}//重置前置加加Maker& operator++(){++this->id;return *this;}//重置后置加加Maker operator++(int)//占位参数,必须是int{Maker tmp(*this);//tmp是局部变量,局部变量不能以引用返回++this->id;return tmp;}
private:int id;
};ostream& operator<<(ostream& out, Maker& m) {cout << m.id << endl;return out;
}void test()
{Maker m1(10);cout << ++m1;cout << m1++;}
int main()
{test();return 0;
}

8、智能指针类

8.1、智能指针类是管理另一个类的对象的释放

#include <iostream>
#include <iostream>
using namespace std;class Maker
{
public:Maker() {cout << "Maker的无参构造" << endl;}~Maker() {cout << "Maker的析构函数" << endl;}};//智能指针类
class SmartPoint
{
public:SmartPoint(Maker* p){this->pMaker = p;}~SmartPoint(){cout << "SmartPoint的析构函数" << endl;if (this->pMaker != NULL){delete this->pMaker;this->pMaker == NULL;}}
private:Maker* pMaker;
};
void test()
{Maker* p = new Maker;//在堆区开辟的数据,需要手动delete掉SmartPoint sm(p);//栈区  会调用析构函数//当test()函数结束时,会调用smartPoint的析构函数。//在这析构函数中delete了Marker的对象,会调用Maker的析构函数
}
int main()
{test();return 0;
}

在这里插入图片描述

8.2、指针运算符重载

#include <iostream>
#include <iostream>
using namespace std;class Maker
{
public:Maker() {cout << "Maker的无参构造" << endl;}void printMaker(){cout << "Hello Maker" << endl;}~Maker() {cout << "Maker的析构函数" << endl;}};//智能指针类
class SmartPoint
{
public:SmartPoint(Maker* p){this->pMaker = p;}//重载指针运算符Maker* operator->(){return this->pMaker;}~SmartPoint(){cout << "SmartPoint的析构函数" << endl;if (this->pMaker != NULL){delete this->pMaker;this->pMaker == NULL;}}
private:Maker* pMaker;
};
void test()
{Maker* p = new Maker;SmartPoint sm(p);sm->printMaker();
}
int main()
{test();return 0;
}

在这里插入图片描述

8.3、重载星号

	//重载星号Maker& operator*(){return *pMaker;}void test()
{Maker* p = new Maker;SmartPoint sm(p);(*sm).printMaker();
}

9、重载函数调用符号

9.1、类里有重载函数调用符号的类实例化的对象也叫仿函数

#include <iostream>
#include <iostream>
using namespace std;//一个类如果重载了函数调用符号,那么这个类实例化出的对象也叫仿函数
//仿函数的作用:1、方便代码维护
class Maker
{
public:Maker(string name) {this->m_Name = name;};void printMaker(){cout << "hello " <<this->m_Name<< endl;}//重载()void operator()(){cout << "hello" << endl;}
public:string m_Name;
};void test()
{Maker func("薯片");func();//看着像函数,但func是对象}
int main()
{test();return 0;
}

相关文章:

C++——运算符重载

1、运算符重载的概念 运算符重载&#xff0c;就是对已有的运算符重新进行定义&#xff0c;赋予其另一种功能&#xff0c;以适应不同的数据类型。运算符重载的目的是让语法更加简洁运算符重载不能改变本来寓意&#xff0c;不能改变基础类型寓意运算符重载的本质是另一种函数调用…...

前端食堂技术周刊第 70 期:Volar 的新开端、Lighthouse 10、良好的组件设计、React 纪录片、2022 大前端总结

美味值&#xff1a;&#x1f31f;&#x1f31f;&#x1f31f;&#x1f31f;&#x1f31f; 口味&#xff1a;黑巧克力 食堂技术周刊仓库地址&#xff1a;https://github.com/Geekhyt/weekly 本期摘要 Volar 的新开端Chrome 110 的新功能Lighthouse 10Nuxt v3.2.0加速 JavaSc…...

react路由详解

在学习react路由之前&#xff0c;我们肯定需要安装路由。大家先运行如下命令安装路由。安装之后随我一起探索react路由。 安装 版本v6 npm i react-router-dom -S 页面准备 创建两个文件夹 pages和 router pages文件夹里面放的是页面 router文件夹里面是进行路由配置 路由…...

mysql数据库完全备份和增量备份与恢复

mysql数据备份&#xff1a; 数据备份方式 物理备份&#xff1a; 冷备&#xff1a;.冷备份指在数据库关闭后,进行备份,适用于所有模式的数据库热备&#xff1a;一般用于保证服务正常不间断运行&#xff0c;用两台机器作为服务机器&#xff0c;一台用于实际数据库操作应用,另外…...

CF1667E Centroid Probabilities

题目描述 对于所有点数为 nnn 的树&#xff0c;如果其满足 对于所有 i∈[2,n]i\in [2,n]i∈[2,n]&#xff0c;与 iii 相连的 jjj 中有且只有一个点 jjj 满足 j<ij<ij<i &#xff0c;那么我们称其为好树 对于 1∼n1\sim n1∼n 每个点求出来有多少好树满足重心为 iii …...

全网详细总结com.alibaba.fastjson.JSONException: syntax error, position at xxx常见错误方式

文章目录1. 复现问题2. 分析问题3. 解决问题4. 该错误的其他解决方法5. 重要补充1. 复现问题 今天在JSONObject.parse(json)这个方法时&#xff0c;却报出如下错误&#xff1a; com.alibaba.fastjson.JSONException: syntax error, position at 0, name usernameat com.aliba…...

快速部署个人导航页:美好的一天从井然有序开始

很多人都习惯使用浏览器自带的收藏夹来管理自己的书签&#xff0c;然而收藏夹存在着一些问题。 经过长时间的累积&#xff0c;一些高频使用的重要网站和偶尔信手收藏的链接混在了一起&#xff0c;收藏夹因为内容过多而显得杂乱无章&#xff1b;收藏夹没有什么美观可言&#xf…...

【Python】如何在 Python 中使用“柯里化”编写干净且可重用的代码

对于中级Python开发者来说&#xff0c;了解了Python的基础语法、库、方法&#xff0c;能够实现一些功能之后&#xff0c;进一步追求的就应该是写出优雅的代码了。 这里介绍一个很有趣的概念“柯里化”。 所谓柯里化&#xff08;Currying&#xff09;是把接受多个参数的函数变换…...

ROS笔记(4)——发布者Publisher与订阅者Subscribe的编程实现

发布者 以小海龟的话题消息为例,编程实现发布者通过/turtle1/cmd_vel 话题向 turtlesim节点发送消息&#xff0c;流程如图 步骤一 创建功能包&#xff08;工作空间为~/catkin_ws/src&#xff09; $ cd ~/catkin_ws/src $ catkin_create_pkg learning_topic roscpp rospy s…...

Linux进程概念(一)

文章目录Linux进程概念&#xff08;一&#xff09;1. 冯诺依曼体系结构2. 操作系统(Operator System)2.1 考虑2.2 如何理解操作系统对硬件做管理&#xff1f;2.3 操作系统为什么要对软硬件资源做管理呢&#xff1f;2.4 系统调用和库函数概念2.5 计算机体系结构3. 进程的初步理解…...

Leetcode.1124 表现良好的最长时间段

题目链接 Leetcode.1124 表现良好的最长时间段 Rating &#xff1a; 1908 题目描述 我们认为当员工一天中的工作小时数大于 8 小时的时候&#xff0c;那么这一天就是「劳累的一天」。 所谓「表现良好的时间段」&#xff0c;意味在这段时间内&#xff0c;「劳累的天数」是严格…...

达梦数据库会话、事务阻塞排查步骤

查询阻塞的事务IDselect * from v$trxwait order by wait_time desc;--单机select * from v$dsc_trxwait order by wait_time desc;–DSC集群查询阻塞事务的会话信息select sf_get_session_sql(sess_id),* from v$sessions where trx_id69667;--单机select sf_get_session_sql(…...

sqlServer 2019 开发版(Developer)下载及安装

下载软件 官网只有2022的&#xff0c;2019使用百度网盘进行下载 安装下崽器 选择自定义安装 选择语言、以及安装位置 点击“安装” 安装 SQL Server 可能的故障 以上步骤安装后会弹出以上界面&#xff0c;如果未弹出&#xff0c;手动去安装目录下点击 SETUP.EXE 文件…...

使用Arthas定位问题

功能概述 首先&#xff0c;Arthas的常用功能大概有以下几个&#xff1a; 解决依赖冲突 sc命令&#xff1a;模糊查看当前 JVM 中是否加载了包含关键字的类&#xff0c;以及获取其完全名称。 sc -d 关键字 注意使用 sc -d 命令&#xff0c;获取 classLoaderHash命令&#xff1a…...

性能测试之tomcat+nginx负载均衡

nginx tomcat 配置准备工作&#xff1a;两个tomcat 执行命令 cp -r apache-tomcat-8.5.56 apache-tomcat-8.5.56_2修改被复制的tomcat2下conf的server.xml 的端口号&#xff0c;不能与tomcat1的端口号重复&#xff0c;不然会启动报错 ,一台电脑上想要启动多个tomcat&#xff0c…...

【手写 Vuex 源码】第十一篇 - Vuex 插件的开发

一&#xff0c;前言 上一篇&#xff0c;主要介绍了 Vuex-namespaced 命名空间的实现&#xff0c;主要涉及以下几个点&#xff1a; 命名空间的介绍和使用&#xff1b;命名空间的逻辑分析与代码实现&#xff1b;命名空间核心流程梳理&#xff1b; 本篇&#xff0c;继续介绍 Vu…...

opencv基础知识和绘图图形

大家好&#xff0c;我是csdn的博主&#xff1a;lqj_本人 这是我的个人博客主页&#xff1a; lqj_本人的博客_CSDN博客-微信小程序,前端,python领域博主lqj_本人擅长微信小程序,前端,python,等方面的知识https://blog.csdn.net/lbcyllqj?spm1011.2415.3001.5343哔哩哔哩欢迎关注…...

15- 决策回归树, 随机森林, 极限森林 (决策树优化) (算法)

1. 决策回归树: from sklearn.tree import DecisionTreeRegressor model DecisionTreeRegressor(criterionmse,max_depth3) model.fit(X,y) # X是40个点 y是一个圆 2. 随机森林 稳定预测: from sklearn.ensemble import RandomForestClassifier # model RandomForestC…...

Flink相关的记录

Flink源码编译首次编译的时候&#xff0c;去除不必要的操作&#xff0c;同时install会把Flink中的module安装到本地仓库&#xff0c;这样依赖当前module的其他组件就无需去远程仓库拉取当前module&#xff0c;节省了时间。mvn clean install -T 4 -DskipTests -Dfast -Dmaven.c…...

配置可视化-基于form-render的无代码配置服务(一)

背景 有些业务场景需要产品或运营去配置JSON数据提供给开发去使用&#xff08;后面有实际业务场景的说明&#xff09;&#xff0c;原有的业务流程&#xff0c;非开发人员&#xff08;后面直接以产品指代&#xff09;把数据交给开发&#xff0c;再由开发去更新JSON数据。对于产…...

业务系统对接大模型的基础方案:架构设计与关键步骤

业务系统对接大模型&#xff1a;架构设计与关键步骤 在当今数字化转型的浪潮中&#xff0c;大语言模型&#xff08;LLM&#xff09;已成为企业提升业务效率和创新能力的关键技术之一。将大模型集成到业务系统中&#xff0c;不仅可以优化用户体验&#xff0c;还能为业务决策提供…...

基于uniapp+WebSocket实现聊天对话、消息监听、消息推送、聊天室等功能,多端兼容

基于 ​UniApp + WebSocket​实现多端兼容的实时通讯系统,涵盖WebSocket连接建立、消息收发机制、多端兼容性配置、消息实时监听等功能,适配​微信小程序、H5、Android、iOS等终端 目录 技术选型分析WebSocket协议优势UniApp跨平台特性WebSocket 基础实现连接管理消息收发连接…...

【位运算】消失的两个数字(hard)

消失的两个数字&#xff08;hard&#xff09; 题⽬描述&#xff1a;解法&#xff08;位运算&#xff09;&#xff1a;Java 算法代码&#xff1a;更简便代码 题⽬链接&#xff1a;⾯试题 17.19. 消失的两个数字 题⽬描述&#xff1a; 给定⼀个数组&#xff0c;包含从 1 到 N 所有…...

高危文件识别的常用算法:原理、应用与企业场景

高危文件识别的常用算法&#xff1a;原理、应用与企业场景 高危文件识别旨在检测可能导致安全威胁的文件&#xff0c;如包含恶意代码、敏感数据或欺诈内容的文档&#xff0c;在企业协同办公环境中&#xff08;如Teams、Google Workspace&#xff09;尤为重要。结合大模型技术&…...

Python爬虫(一):爬虫伪装

一、网站防爬机制概述 在当今互联网环境中&#xff0c;具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类&#xff1a; 身份验证机制&#xff1a;直接将未经授权的爬虫阻挡在外反爬技术体系&#xff1a;通过各种技术手段增加爬虫获取数据的难度…...

【C语言练习】080. 使用C语言实现简单的数据库操作

080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

智能分布式爬虫的数据处理流水线优化:基于深度强化学习的数据质量控制

在数字化浪潮席卷全球的今天&#xff0c;数据已成为企业和研究机构的核心资产。智能分布式爬虫作为高效的数据采集工具&#xff0c;在大规模数据获取中发挥着关键作用。然而&#xff0c;传统的数据处理流水线在面对复杂多变的网络环境和海量异构数据时&#xff0c;常出现数据质…...

面向无人机海岸带生态系统监测的语义分割基准数据集

描述&#xff1a;海岸带生态系统的监测是维护生态平衡和可持续发展的重要任务。语义分割技术在遥感影像中的应用为海岸带生态系统的精准监测提供了有效手段。然而&#xff0c;目前该领域仍面临一个挑战&#xff0c;即缺乏公开的专门面向海岸带生态系统的语义分割基准数据集。受…...

C++:多态机制详解

目录 一. 多态的概念 1.静态多态&#xff08;编译时多态&#xff09; 二.动态多态的定义及实现 1.多态的构成条件 2.虚函数 3.虚函数的重写/覆盖 4.虚函数重写的一些其他问题 1&#xff09;.协变 2&#xff09;.析构函数的重写 5.override 和 final关键字 1&#…...

深入理解Optional:处理空指针异常

1. 使用Optional处理可能为空的集合 在Java开发中&#xff0c;集合判空是一个常见但容易出错的场景。传统方式虽然可行&#xff0c;但存在一些潜在问题&#xff1a; // 传统判空方式 if (!CollectionUtils.isEmpty(userInfoList)) {for (UserInfo userInfo : userInfoList) {…...