news 2026/3/30 17:17:15

C++ 运算符重载详解:赋值与取地址运算符及日期类实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++ 运算符重载详解:赋值与取地址运算符及日期类实现

比如说,两个日期类相加就没有意义,两个日期类相减就可以算一个天数了。这个运算符具体在什么场景下到底有没有意义,怎么来算都要程序员来定,所以基于这样的原因,自定义类型是不支持使用各种运算符的

这里想要支持的话,C++之父也设置了一套机制

  • 当运算符被用于类类型(也就是自定义类型)的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使用运算符时,必须转换成调用对应的运算符重载,若是没有对应的运算符重载,则会编译报错,也就说默认是不支持的,但是实现一个运算符重载的函数,就支持了。注意这里运算符重载和函数重载没什么关联
  • 运算符重载是具有特殊名字的函数,他的名字是由operator(关键字,本身就具有重载的意思)和后面要定义的运算符共同构成。和其他函数一样,它也具有其返回类型和参数列表以及函数体。
  • 重载运算符的参数个数和该运算符作用的运算对象数量一样多。一元运算符(一个操作数)有一个参数,二元运算符有两个参数,二元运算符的左侧运算对象传给第一个参数,右侧运算对象传给第二个参数(最好顺序不要乱,乘加等于就没有影响,减,除,比较就有影响了)。

在这里插入图片描述

在这里插入图片描述

但是没有显式调用的必要

代码语言:javascript

AI代码解释

int main() { int i = 0, j = 1;//内置类型就转换成对应的指令 bool ret = i == j;

如果这里不接收i==j的结果,比较没有意义,此时转到反汇编指令就会被优化

在这里插入图片描述

这里对应的指令是cmp,cmp就是compare,将i给eax,eax就是i的值,然后和j进行一个cmp比较,后面指令的意思就是取到比较的结果

在这里插入图片描述

这里也确实看到对于自定义类型要转换为调用对应的函数

这里的代码还有很多缺点,都知道传值传参是要调用拷贝构造的,对于内置类型,用传值传参和引用传参区别不大,第一内置类型都比较小,传值传参拷贝代价也不大,第二它也不涉及调用拷贝构造,都是指令直接完成。但是对于自定义类型,尤其是深拷贝的类型,用引用传参就非常的重要,而且这里函数不改变参数,加上const更好,这样普通对象,const对象,都可以调用

map.qhkrwom.cn/Blog/579959.shtml
map.qhkrwom.cn/Blog/791597.shtml
map.qhkrwom.cn/Blog/775139.shtml
map.qhkrwom.cn/Blog/868442.shtml
map.qhkrwom.cn/Blog/000604.shtml
map.qhkrwom.cn/Blog/175559.shtml
map.qhkrwom.cn/Blog/395159.shtml
map.qhkrwom.cn/Blog/379599.shtml
map.qhkrwom.cn/Blog/539935.shtml
map.qhkrwom.cn/Blog/086808.shtml
map.qhkrwom.cn/Blog/537559.shtml
map.qhkrwom.cn/Blog/911319.shtml
map.qhkrwom.cn/Blog/971759.shtml
map.qhkrwom.cn/Blog/955773.shtml
map.qhkrwom.cn/Blog/737157.shtml
map.qhkrwom.cn/Blog/913591.shtml
map.qhkrwom.cn/Blog/575319.shtml
map.qhkrwom.cn/Blog/713519.shtml
map.qhkrwom.cn/Blog/139731.shtml
map.qhkrwom.cn/Blog/577131.shtml
map.qhkrwom.cn/Blog/971753.shtml
map.qhkrwom.cn/Blog/113159.shtml
map.qhkrwom.cn/Blog/199375.shtml
map.qhkrwom.cn/Blog/373571.shtml
map.qhkrwom.cn/Blog/686860.shtml
map.qhkrwom.cn/Blog/800204.shtml
map.qhkrwom.cn/Blog/193159.shtml
map.qhkrwom.cn/Blog/573171.shtml
map.qhkrwom.cn/Blog/917153.shtml
map.qhkrwom.cn/Blog/746517.shtml
map.qhkrwom.cn/Blog/482026.shtml
map.qhkrwom.cn/Blog/353591.shtml
map.qhkrwom.cn/Blog/593711.shtml
map.qhkrwom.cn/Blog/317919.shtml
map.qhkrwom.cn/Blog/171719.shtml
map.qhkrwom.cn/Blog/579751.shtml
map.qhkrwom.cn/Blog/775955.shtml
map.qhkrwom.cn/Blog/911759.shtml
map.qhkrwom.cn/Blog/412777.shtml
map.qhkrwom.cn/Blog/139733.shtml
map.qhkrwom.cn/Blog/408002.shtml
map.qhkrwom.cn/Blog/511553.shtml
map.qhkrwom.cn/Blog/317795.shtml
map.qhkrwom.cn/Blog/597311.shtml
map.qhkrwom.cn/Blog/715715.shtml
map.qhkrwom.cn/Blog/240482.shtml
map.qhkrwom.cn/Blog/173775.shtml
map.qhkrwom.cn/Blog/997755.shtml
map.qhkrwom.cn/Blog/337519.shtml
map.qhkrwom.cn/Blog/460426.shtml
map.qhkrwom.cn/Blog/117359.shtml
map.qhkrwom.cn/Blog/533973.shtml
map.qhkrwom.cn/Blog/355111.shtml
map.qhkrwom.cn/Blog/464802.shtml
map.qhkrwom.cn/Blog/977359.shtml
map.qhkrwom.cn/Blog/331753.shtml
map.qhkrwom.cn/Blog/917113.shtml
map.qhkrwom.cn/Blog/373793.shtml
map.qhkrwom.cn/Blog/999971.shtml
map.qhkrwom.cn/Blog/955753.shtml
map.qhkrwom.cn/Blog/195533.shtml
map.qhkrwom.cn/Blog/917191.shtml
map.qhkrwom.cn/Blog/822080.shtml
map.qhkrwom.cn/Blog/937175.shtml
map.qhkrwom.cn/Blog/711793.shtml
map.qhkrwom.cn/Blog/155973.shtml
map.qhkrwom.cn/Blog/735313.shtml
map.qhkrwom.cn/Blog/735191.shtml
map.qhkrwom.cn/Blog/571591.shtml
map.qhkrwom.cn/Blog/737715.shtml
map.qhkrwom.cn/Blog/113991.shtml
map.qhkrwom.cn/Blog/519135.shtml
map.qhkrwom.cn/Blog/591577.shtml
map.qhkrwom.cn/Blog/535571.shtml
map.qhkrwom.cn/Blog/591775.shtml
map.qhkrwom.cn/Blog/171555.shtml
map.qhkrwom.cn/Blog/753133.shtml
map.qhkrwom.cn/Blog/719973.shtml
map.qhkrwom.cn/Blog/395173.shtml
map.qhkrwom.cn/Blog/777151.shtml
map.qhkrwom.cn/Blog/066008.shtml
map.qhkrwom.cn/Blog/224246.shtml
map.qhkrwom.cn/Blog/937195.shtml
map.qhkrwom.cn/Blog/157333.shtml
map.qhkrwom.cn/Blog/048424.shtml
map.qhkrwom.cn/Blog/462686.shtml
map.qhkrwom.cn/Blog/991795.shtml
map.qhkrwom.cn/Blog/535539.shtml
map.qhkrwom.cn/Blog/131579.shtml
map.qhkrwom.cn/Blog/751953.shtml
map.qhkrwom.cn/Blog/571351.shtml
map.qhkrwom.cn/Blog/533955.shtml
map.qhkrwom.cn/Blog/533711.shtml
map.qhkrwom.cn/Blog/375119.shtml
map.qhkrwom.cn/Blog/226224.shtml
map.qhkrwom.cn/Blog/393313.shtml
map.qhkrwom.cn/Blog/979917.shtml
map.qhkrwom.cn/Blog/373579.shtml
map.qhkrwom.cn/Blog/957997.shtml
map.qhkrwom.cn/Blog/535935.shtml
map.qhkrwom.cn/Blog/597155.shtml
map.qhkrwom.cn/Blog/755175.shtml
map.qhkrwom.cn/Blog/791177.shtml
map.qhkrwom.cn/Blog/533537.shtml
map.qhkrwom.cn/Blog/313311.shtml
map.qhkrwom.cn/Blog/991737.shtml
map.qhkrwom.cn/Blog/959935.shtml
map.qhkrwom.cn/Blog/997597.shtml
map.qhkrwom.cn/Blog/737191.shtml
map.qhkrwom.cn/Blog/111179.shtml
map.qhkrwom.cn/Blog/773797.shtml
map.qhkrwom.cn/Blog/337117.shtml
map.qhkrwom.cn/Blog/737995.shtml
map.qhkrwom.cn/Blog/999955.shtml
map.qhkrwom.cn/Blog/779113.shtml
map.qhkrwom.cn/Blog/515799.shtml
map.qhkrwom.cn/Blog/153777.shtml
map.qhkrwom.cn/Blog/195193.shtml
map.qhkrwom.cn/Blog/753139.shtml
map.qhkrwom.cn/Blog/355375.shtml
map.qhkrwom.cn/Blog/753173.shtml
map.qhkrwom.cn/Blog/991157.shtml
map.qhkrwom.cn/Blog/139339.shtml
map.qhkrwom.cn/Blog/399733.shtml
map.qhkrwom.cn/Blog/313997.shtml
map.qhkrwom.cn/Blog/717991.shtml
map.qhkrwom.cn/Blog/199113.shtml
map.qhkrwom.cn/Blog/537557.shtml
map.qhkrwom.cn/Blog/137991.shtml
map.qhkrwom.cn/Blog/755311.shtml
map.qhkrwom.cn/Blog/339333.shtml
map.qhkrwom.cn/Blog/195991.shtml
map.qhkrwom.cn/Blog/999179.shtml
map.qhkrwom.cn/Blog/955337.shtml
map.qhkrwom.cn/Blog/937397.shtml
map.qhkrwom.cn/Blog/359399.shtml
map.qhkrwom.cn/Blog/755733.shtml
map.qhkrwom.cn/Blog/913197.shtml
map.qhkrwom.cn/Blog/591137.shtml
map.qhkrwom.cn/Blog/555379.shtml
map.qhkrwom.cn/Blog/797759.shtml
map.qhkrwom.cn/Blog/557711.shtml
map.qhkrwom.cn/Blog/997939.shtml
map.qhkrwom.cn/Blog/753733.shtml
map.qhkrwom.cn/Blog/773713.shtml
map.qhkrwom.cn/Blog/959973.shtml
map.qhkrwom.cn/Blog/915513.shtml
map.qhkrwom.cn/Blog/535997.shtml
map.qhkrwom.cn/Blog/759935.shtml
map.qhkrwom.cn/Blog/551773.shtml
map.qhkrwom.cn/Blog/939973.shtml
map.qhkrwom.cn/Blog/157757.shtml
map.qhkrwom.cn/Blog/311511.shtml
map.qhkrwom.cn/Blog/593955.shtml
map.qhkrwom.cn/Blog/713115.shtml
map.qhkrwom.cn/Blog/591593.shtml
map.qhkrwom.cn/Blog/155353.shtml
map.qhkrwom.cn/Blog/753313.shtml
map.qhkrwom.cn/Blog/519199.shtml
map.qhkrwom.cn/Blog/335731.shtml
map.qhkrwom.cn/Blog/375913.shtml
map.qhkrwom.cn/Blog/379557.shtml
map.qhkrwom.cn/Blog/537979.shtml
map.qhkrwom.cn/Blog/153117.shtml
map.qhkrwom.cn/Blog/951933.shtml
map.qhkrwom.cn/Blog/957155.shtml
map.qhkrwom.cn/Blog/153779.shtml
map.qhkrwom.cn/Blog/004224.shtml
map.qhkrwom.cn/Blog/397191.shtml
map.qhkrwom.cn/Blog/939359.shtml
map.qhkrwom.cn/Blog/006008.shtml
map.qhkrwom.cn/Blog/379713.shtml
map.qhkrwom.cn/Blog/775797.shtml
map.qhkrwom.cn/Blog/355951.shtml
map.qhkrwom.cn/Blog/359919.shtml
map.qhkrwom.cn/Blog/177379.shtml
map.qhkrwom.cn/Blog/828220.shtml
map.qhkrwom.cn/Blog/953771.shtml
map.qhkrwom.cn/Blog/171915.shtml
map.qhkrwom.cn/Blog/391777.shtml
map.qhkrwom.cn/Blog/533135.shtml
map.qhkrwom.cn/Blog/755533.shtml
map.qhkrwom.cn/Blog/959955.shtml
map.qhkrwom.cn/Blog/951515.shtml
map.qhkrwom.cn/Blog/931551.shtml
map.qhkrwom.cn/Blog/311153.shtml
map.qhkrwom.cn/Blog/482868.shtml
map.qhkrwom.cn/Blog/731339.shtml
map.qhkrwom.cn/Blog/131757.shtml
map.qhkrwom.cn/Blog/577379.shtml
map.qhkrwom.cn/Blog/739573.shtml
map.qhkrwom.cn/Blog/991133.shtml
map.qhkrwom.cn/Blog/377959.shtml
map.qhkrwom.cn/Blog/462024.shtml
map.qhkrwom.cn/Blog/515597.shtml
map.qhkrwom.cn/Blog/957395.shtml
map.qhkrwom.cn/Blog/595731.shtml
map.qhkrwom.cn/Blog/626444.shtml
map.qhkrwom.cn/Blog/268444.shtml
map.qhkrwom.cn/Blog/959337.shtml
map.qhkrwom.cn/Blog/537793.shtml
map.qhkrwom.cn/Blog/595777.shtml
map.qhkrwom.cn/Blog/977371.shtml
map.qhkrwom.cn/Blog/713519.shtml
map.qhkrwom.cn/Blog/371197.shtml
map.qhkrwom.cn/Blog/315355.shtml
map.qhkrwom.cn/Blog/579599.shtml
map.qhkrwom.cn/Blog/484282.shtml
map.qhkrwom.cn/Blog/177555.shtml
map.qhkrwom.cn/Blog/751931.shtml
map.qhkrwom.cn/Blog/951195.shtml
map.qhkrwom.cn/Blog/913537.shtml
map.qhkrwom.cn/Blog/864486.shtml
map.qhkrwom.cn/Blog/139737.shtml
map.qhkrwom.cn/Blog/795171.shtml
map.qhkrwom.cn/Blog/793391.shtml
map.qhkrwom.cn/Blog/593955.shtml
map.qhkrwom.cn/Blog/979173.shtml
map.qhkrwom.cn/Blog/000680.shtml
map.qhkrwom.cn/Blog/395395.shtml
map.qhkrwom.cn/Blog/848428.shtml
map.qhkrwom.cn/Blog/597153.shtml
map.qhkrwom.cn/Blog/191751.shtml
map.qhkrwom.cn/Blog/971717.shtml
map.qhkrwom.cn/Blog/240802.shtml
map.qhkrwom.cn/Blog/555379.shtml
map.qhkrwom.cn/Blog/448482.shtml
map.qhkrwom.cn/Blog/575937.shtml
map.qhkrwom.cn/Blog/551573.shtml
map.qhkrwom.cn/Blog/002280.shtml
map.qhkrwom.cn/Blog/537535.shtml
map.qhkrwom.cn/Blog/359173.shtml
map.qhkrwom.cn/Blog/157519.shtml
map.qhkrwom.cn/Blog/686640.shtml
map.qhkrwom.cn/Blog/044406.shtml
map.qhkrwom.cn/Blog/591753.shtml
map.qhkrwom.cn/Blog/137111.shtml
map.qhkrwom.cn/Blog/884286.shtml
map.qhkrwom.cn/Blog/113777.shtml
map.qhkrwom.cn/Blog/555971.shtml
map.qhkrwom.cn/Blog/951551.shtml
map.qhkrwom.cn/Blog/715599.shtml
map.qhkrwom.cn/Blog/579715.shtml
map.qhkrwom.cn/Blog/559171.shtml
map.qhkrwom.cn/Blog/717971.shtml
map.qhkrwom.cn/Blog/795397.shtml
map.qhkrwom.cn/Blog/779971.shtml
map.qhkrwom.cn/Blog/757995.shtml
map.qhkrwom.cn/Blog/953113.shtml
map.qhkrwom.cn/Blog/791919.shtml
map.qhkrwom.cn/Blog/733733.shtml
map.qhkrwom.cn/Blog/486064.shtml
map.qhkrwom.cn/Blog/955597.shtml
map.qhkrwom.cn/Blog/357517.shtml
map.qhkrwom.cn/Blog/951719.shtml
map.qhkrwom.cn/Blog/377759.shtml
map.qhkrwom.cn/Blog/006280.shtml
map.qhkrwom.cn/Blog/331553.shtml
map.qhkrwom.cn/Blog/515931.shtml
map.qhkrwom.cn/Blog/597339.shtml
map.qhkrwom.cn/Blog/539353.shtml
map.qhkrwom.cn/Blog/804602.shtml
map.qhkrwom.cn/Blog/600806.shtml
map.qhkrwom.cn/Blog/119377.shtml
map.qhkrwom.cn/Blog/999313.shtml
map.qhkrwom.cn/Blog/028668.shtml
map.qhkrwom.cn/Blog/999751.shtml
map.qhkrwom.cn/Blog/646000.shtml
map.qhkrwom.cn/Blog/442282.shtml
map.qhkrwom.cn/Blog/917535.shtml
map.qhkrwom.cn/Blog/119539.shtml
map.qhkrwom.cn/Blog/591975.shtml
map.qhkrwom.cn/Blog/533779.shtml
map.qhkrwom.cn/Blog/042862.shtml
map.qhkrwom.cn/Blog/971757.shtml
map.qhkrwom.cn/Blog/719771.shtml
map.qhkrwom.cn/Blog/717959.shtml
map.qhkrwom.cn/Blog/771179.shtml
map.qhkrwom.cn/Blog/137579.shtml
map.qhkrwom.cn/Blog/153511.shtml
map.qhkrwom.cn/Blog/993711.shtml
map.qhkrwom.cn/Blog/111531.shtml
map.qhkrwom.cn/Blog/066006.shtml
map.qhkrwom.cn/Blog/319753.shtml
map.qhkrwom.cn/Blog/573175.shtml
map.qhkrwom.cn/Blog/337199.shtml
map.qhkrwom.cn/Blog/315713.shtml
map.qhkrwom.cn/Blog/977935.shtml
map.qhkrwom.cn/Blog/351115.shtml
map.qhkrwom.cn/Blog/957335.shtml

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/24 11:07:12

SORAV2网页驱动:AI如何革新网页自动化开发

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个基于SORAV2网页驱动的AI辅助开发工具,能够根据用户输入的网页URL自动生成自动化测试脚本。功能包括:1. 自动分析网页结构并识别关键元素&#xff1…

作者头像 李华
网站建设 2026/3/18 4:34:18

快速验证:MAT下载与原型开发

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个MAT文件下载与处理的快速原型工具。用户输入MAT文件URL后,工具快速下载文件并解析数据,生成一个简单的数据摘要和可视化图表。支持快速迭代和功能扩…

作者头像 李华
网站建设 2026/3/14 16:49:22

小红书数据采集新纪元:零门槛获取无水印内容的智能方案

小红书数据采集新纪元:零门槛获取无水印内容的智能方案 【免费下载链接】XHS-Downloader 免费;轻量;开源,基于 AIOHTTP 模块实现的小红书图文/视频作品采集工具 项目地址: https://gitcode.com/gh_mirrors/xh/XHS-Downloader …

作者头像 李华
网站建设 2026/3/26 20:50:13

ENSP零基础入门:从下载到第一个实验

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 生成一个面向新手的ENSP入门教程,包含:1) ENSP下载和安装的详细步骤图解;2) 软件界面各功能区说明;3) 第一个实验两台PC互ping的完整…

作者头像 李华
网站建设 2026/3/26 9:09:44

1小时验证创意:AE视频片段原型开发指南

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个快速AE原型生成器,特点:1) 输入创意描述自动生成多个风格选项 2) 极简参数调节(时长、主色调、节奏) 3) 低精度快速渲染 4)…

作者头像 李华
网站建设 2026/3/24 22:01:44

AI一键生成SSL证书下载工具,告别繁琐命令

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个SSL证书下载工具,功能包括:1. 用户输入域名后自动检测SSL证书信息 2. 支持CHLS.PRO.SSL格式证书下载 3. 自动生成证书安装指南 4. 提供证书有效期提…

作者头像 李华