news 2026/6/10 16:53:37

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/6/10 10:12:09

C++ 类的默认成员函数详解:构造、析构与拷贝构造

第一:我们不写时,编译器默认生成的函数行为是什么,是否满足我们的需求编译器默认生成的函数不满足我们的需求,我们需要自己实现,那么如何自己实现?在这里插入图片描述二、构造函数构造函数是特殊的成员函数…

作者头像 李华
网站建设 2026/6/10 0:52:35

VibeVoice集成大语言模型,实现上下文感知的语音合成

VibeVoice集成大语言模型,实现上下文感知的语音合成 在播客、有声书和虚拟访谈日益流行的内容生态中,一个长期困扰开发者的问题是:如何让AI生成的语音听起来不像“机器念稿”,而更像一场真实、自然的人类对话?传统的文…

作者头像 李华
网站建设 2026/6/10 11:19:35

AI一键搞定Jupyter Notebook安装,告别复杂配置

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个AI辅助的Jupyter Notebook安装工具。功能包括:1.自动检测用户操作系统类型和版本 2.根据系统环境生成定制化的安装脚本 3.自动安装Python和必要依赖项 4.配置J…

作者头像 李华
网站建设 2026/6/10 15:02:30

效率坊实战:用AI解决实际开发难题

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个实战应用,展示效率坊在数据处理中的能力。功能包括:从CSV或Excel导入数据,通过自然语言描述生成数据处理脚本(如过滤、排序…

作者头像 李华
网站建设 2026/6/10 11:35:54

电商系统MySQL实战:从安装到高可用集群搭建

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个电商系统MySQL部署模拟器,包含:1. 单节点安装演示 2. 主从复制配置向导 3. 读写分离中间件集成 4. 压力测试模块 5. 性能监控面板。要求使用Docker…

作者头像 李华
网站建设 2026/6/10 11:39:55

文本转图片工具如何提升内容创作效率300%

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个效率对比工具,功能包括:1. 传统设计流程时间记录器 2. AI文本转图片用时统计 3. 质量评估系统(分辨率、美观度等) 4. 批量处理性能测试 5. 生成详细…

作者头像 李华