news 2026/5/4 20:53:58

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/4/30 11:22:15

深入理解vector:模拟实现与现代C++技巧

、vector的模拟实现1.1 resize在这里插入图片描述接口作用&#xff1a;当n < 当前size时&#xff1a;会截断为前n个元素&#xff0c;超出n的元素会被移除并销毁。当当前size < n ≤ 当前capacity时&#xff1a;在容器末尾插入足够的元素&#xff0c;使总个数达到n。新元素…

作者头像 李华
网站建设 2026/4/25 17:20:49

LVGL中字体嵌入方法:超详细版配置流程

LVGL字体嵌入实战指南&#xff1a;从零打造个性化中文界面 你有没有遇到过这样的场景&#xff1f;产品要出海&#xff0c;UI里却连“Привет”都显示成方块&#xff1b;或者客户拿着品牌VI手册说&#xff1a;“这个按钮的字体必须用我们定制的汉仪旗黑&#xff0c;不能妥…

作者头像 李华
网站建设 2026/5/1 4:45:21

告别依赖地狱:Anaconda如何提升开发效率300%

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个对比演示项目&#xff1a;1) 展示传统pip安装多个机器学习库时出现的依赖冲突问题&#xff1b;2) 使用Anaconda创建独立环境完美解决&#xff1b;3) 对比两种方式的安装时…

作者头像 李华
网站建设 2026/4/29 19:44:12

10分钟搭建VUE面试模拟器原型

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 快速开发一个VUE面试模拟器MVP&#xff0c;功能包括&#xff1a;1. 随机抽取面试题功能 2. 倒计时回答界面 3. 简易代码编辑器 4. 参考答案对比 5. 基础评分系统。要求1小时内可完…

作者头像 李华
网站建设 2026/4/30 0:53:07

Excel小白必看:VLOOKUP跨表匹配5分钟入门

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 制作一个面向新手的VLOOKUP跨表匹配教学工具。要求&#xff1a;1. 提供两个简单的示例表格&#xff1b;2. 分步演示VLOOKUP公式的编写过程&#xff1b;3. 包含常见错误及解决方法&…

作者头像 李华
网站建设 2026/5/2 3:21:02

AI如何简化KETTLE下载与ETL流程开发

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个Python脚本&#xff0c;使用KETTLE API自动下载并配置Pentaho Data Integration工具。脚本应包含以下功能&#xff1a;1.自动检测操作系统类型并下载对应版本的KETTLE&…

作者头像 李华