【C++】 类与对象(二)(2.运算符重载)
目录赋值运算符重载1.运算符重载2.赋值运算符重载3.日期类实现取地址运算符重载1.const 成员2.取地址运算符重载运算符重构operator运算符重构是对类这和算符的适配性进行一个适配的重构就是对于平常的运算符无法进行加减乘除等等还有赋值操作符进行一个新的从新修改。而在C中就允许我们可以进行对运算符进行从新修改操作对于运算符重构还需要有一个重要字operator在这个关键字的后面直接加上对应的运算符像 -- -等等除了.* :: sizeo() . ?:)这5个运算符无法修改。赋值运算符重载1.运算符重载运算符重载的注意点1必须是有一个类相关的传值如果没有就变成重构原本的运算的意义//error int operator(int a,int b) { return a-b; }2不能重构没有意义的运算符如operator加减运算符重载正确示范以 Date 日期类为例日期类的运算符重载实现/*传年月取月份总天数*/ int Date::GetMonthDay(int year, int month) { //最基础的实现一一对应每个月份的数组传值为了不浪费一个空间把0位置改为闰年2月份的存储位置 static const int month_day[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //2月份 闰年的判断 if (month 2 ((year % 4 0 year % 100 ! 0) || year % 400 0)) { return 29; } else { return month_day[month]; } } Date Date::operator(int day) { //判断如果传过来的day 是否为负数如果为负数就给-处理注意一定要改day的正负数 if (day 0) { return *this - -day; } //先进行加减 _day day; //修改直到day为正确的日期为止 注意不要号会导致出现0号情况 while (_day GetMonthDay(_year, _month)) { _day - GetMonthDay(_year, _month); if (_month 13) { _month 1; _year; } } return *this; }注意对应的*this 在函数销毁时*this 的生命周期还在所以可以用类类型的应用进行返回。日期类的运算符重载实现//利用已有的实现 Date Date::operator(int day) { // 的实现是通过原数值没有更改 所以进行拷贝出来的实例化对象用这个实例化对象进行操作 Date tmp *this; return tmp day; } 的实现是通过原数值没有更改 所以进行拷贝出来的实例化对象用这个实例化对象进行操作注意所拷贝出来的对象是一个临时对象这个不能用Date 的类类型的引用返回值只能用类类型即可。日期类的- -运算符重载实现与 是同理的Date Date::operator-(int day) { //判断如果传过来的day 是否为负数如果为负数就给处理注意一定要改day的正负数 if (day 0) { return *this -day; } //先进行加减 _day - day; //修改直到day为正确的日期为止 注意这里与不同需要0,还是那个问题防止出现0号的问题 while (_day 0) { _day GetMonthDay(_year, _month--); if (_month 0) { _month 12; _year--; } } return *this; } Date Date::operator-(int day) { // - 的实现是通过原数值没有更改 所以进行拷贝出来的实例化对象用这个实例化对象进行-操作 Date tmp *this; return tmp - day; }注意点返回值需不需要引用的问题还是考虑是不是临时对象传值后会不会消亡 --的前置后置更改的实现在 和-- 都有前置与后置问题但是运算符有时一模一样就没有办法区分所以C就进行了一个特殊处理有传参的运算符就是后置没有的表示前置一定要int 只要有传参即可区分。operator()//前置 operator(int)//后置 operator--()//前置 operator--(int)//后置代码展示Date Date::operator()//后置 { *this 1; return*this; } Date Date::operator(int)//前置 { Date tmp *this; *this 1; return tmp; } Date Date::operator--()//后置 { *this - 1; return*this; } Date Date::operator--(int)//前置 { Date tmp *this; *this - 1; return tmp; }比较运算符重载 ! bool Date::operator(const Date dat) { return _year dat._year _month dat._month _day dat._day; } bool Date::operator!(const Date dat) { return !(*this dat); } bool Date::operator(const Date dat) { if (_year dat._year) return true; if (_year dat._year _month dat._month) return true; if (_year dat._year _month dat._month _day dat._day) return true; return false; } bool Date::operator(const Date dat) { return !(*this dat); } bool Date::operator(const Date dat) { if (_year dat._year) return true; if (_year dat._year _month dat._month) return true; if (_year dat._year _month dat._month _day dat._day) return true; return false; } bool Date::operator(const Date dat) { return !(*this dat); }其实主要实现的 这三个其他都是利用了!取反的运算符进行实现的。练习题两个日期相减得出两天之间差距多少天。答案在日期类实现中/*天数相减计算天数*/ int operator-(const Date date);2.赋值运算符重载Date tmp *this; Date tmp(*this);上面的代码都是等效的这两个代码都是利用拷贝构造函数实现的但所谓的赋值运算就是比已经实力化对象进行赋值操作就得重新对赋值进行重构操作。int main() { //实例化拷贝构造 Date a(2026, 8, 9); Date b a; b.Print(); //赋值运算 Date c(2026, 8, 9); Date d; d c; d.Print(); return 0; }Date Date::operator(const Date d) { if (*this ! d) { _year d._year; _month d._month; _day d._day; } return *this; }建议在传参加const 防止误操返回值要使用类类型的应用可以支持连续赋值场景在没有赋值重构的时候编译器自动生成一个赋值传参进行一个字节一个字节的拷贝支配性可能容易出现环境不适配的问题Stack 等这种就一定需要赋值重载的情况。3.日期类实现/*天数相减计算天数*/答案不唯一原理实现即可 int Date::operator-(const Date date) { Date tmp *this; int day 0, fu 1; if (*this date) fu -1; while (tmp ! date) { day fu; tmp fu; } return fu*day; }//Data.h #pragma once #include iostream #include stdlib.h #include stdbool.h #include assert.h using namespace std; class Date { public: Date(int year 1, int month 1, int day 1);//构造函数缺省 Date(Date date);//拷贝构造函数 Date(const Date date); void Print();//输出 int GetMonthDay(int year, int month);//取月天数 //运算符重载 /*加减天数改变天数*/ Date operator(int day); Date operator(int day); Date operator-(int day); Date operator-(int day); /* -- 的前置后置更改的实现*/ Date operator();//前置 Date operator(int);//后置 Date operator--();//前置 Date operator--(int);//后置 /*比较天数*/ bool operator(const Date dat)const; bool operator!(const Date dat)const; bool operator(const Date dat)const; bool operator(const Date dat)const; bool operator(const Date dat)const; bool operator(const Date dat)const; /*天数相减计算天数*/ int operator-(const Date date); /*赋值运算符*/ Date operator(const Date d); /*取地址运算符重构*/ Date* operator(); const Date* operator()const; private: int _year; int _month; int _day; };//Data.cpp #define _CRT_SECURE_NO_WARNINGS #include Data.h /************************************* 基本的函数 **********************************************/ //构造 Date::Date(int year, int month, int day) { _year year; _month month; _day day; } //拷贝构造 Date::Date(Date date) { _year date._year; _month date._month; _day date._day; } Date::Date(const Date date) { _year date._year; _month date._month; _day date._day; } /*传年月取月份总天数*/ int Date::GetMonthDay(int year, int month) { static const int month_day[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (month 2 ((year % 4 0 year % 100 ! 0) || year % 400 0)) { return 29; } else { return month_day[month]; } } /*输出函数*/ void Date::Print() { cout _year / _month / _day endl; } /********************************** 运算符重载 ***********************************************/ /*加减天数改变天数*/ Date Date::operator(int day) { if (day 0) { return *this - -day; } _day day; while (_day GetMonthDay(_year, _month)) { _day - GetMonthDay(_year, _month); if (_month 13) { _month 1; _year; } } return *this; } Date Date::operator(int day) { Date tmp *this; return tmp day; } Date Date::operator-(int day) { if (day 0) { return *this -day; } _day - day; while (_day 0) { _day GetMonthDay(_year, _month--); if (_month 0) { _month 12; _year--; } } return *this; } Date Date::operator-(int day) { Date tmp *this; return tmp - day; } /* -- 的前置后置更改的实现*/ Date Date::operator()//前置 { *this 1; return*this; } Date Date::operator(int)//后置 { Date tmp *this; *this 1; return tmp; } Date Date::operator--()//前置 { *this - 1; return*this; } Date Date::operator--(int)//后置 { Date tmp *this; *this - 1; return tmp; } /*比较天数*/ bool Date::operator(const Date dat) const { return _year dat._year _month dat._month _day dat._day; } bool Date::operator!(const Date dat) const { return !(*this dat); } bool Date::operator(const Date dat) const { if (_year dat._year) return true; if (_year dat._year _month dat._month) return true; if (_year dat._year _month dat._month _day dat._day) return true; return false; } bool Date::operator(const Date dat) const { return !(*this dat); } bool Date::operator(const Date dat) const { if (_year dat._year) return true; if (_year dat._year _month dat._month) return true; if (_year dat._year _month dat._month _day dat._day) return true; return false; } bool Date::operator(const Date dat) const { return !(*this dat); } /*天数相减计算天数*/ int Date::operator-(const Date date) { Date tmp *this; int day 0, fu 1; if (*this date) fu -1; while (tmp ! date) { day fu; tmp (fu); } return fu*day; } /*赋值运算符*/ Date Date::operator(const Date d) { if (*this ! d) { _year d._year; _month d._month; _day d._day; } return *this; } /*取地址运算符重构*/ Date* Date::operator() { return this; } const Date* Date::operator()const { return this; } int main() { return 0; }取地址运算符重载1.const 成员传参参数可以加const 进行防止误操的情况但我们*this这个时在类里面的我们在调用的时候也可以进行更改但在用有一些函数的时候我们不需要进行修改*this的情况但想要加const就变成个问题所以在C中就提供了一个解决方案在函数括号后加const 进行限定*this。bool Date::operator(const Date dat) const2.取地址运算符重载跟地址有关的有 [] 这两个所也可以通过重构进行。Date* Date::operator() { return this; } const Date* Date::operator()const { return this; }//test.h class Arr { public: Arr(int n4);//构造 int operator[](int n); ~Arr(); private: int* _arr; }; //test.cpp Arr::Arr(int n) { _arr (int*)malloc(sizeof(int) * n); if (_arr NULL) { perror(malloc); exit(1); } } int Arr::operator[](int n) { return _arr[n]; } Arr::~Arr() { free(_arr); _arr NULL; }感谢观看

相关新闻

Gophercloud 实战指南:使用 Go SDK 完成 OpenStack 认证与云资源编排

Gophercloud 实战指南:使用 Go SDK 完成 OpenStack 认证与云资源编排

测试云原生质量保障 【免费下载链接】origin Conformance test suite for OpenShift 项目地址: https://gitcode.com/gh_mirrors/or/origin 点击查看 免费下载 本指南以 Gophercloud 官方 README 为核心,结合本仓库(OpenShift Conformance t…

2026/9/28 3:40:53 阅读更多 →
HTTP 与 HTTPS 跳转故障:循环重定向与混合内容排查

HTTP 与 HTTPS 跳转故障:循环重定向与混合内容排查

HTTP 与 HTTPS 跳转故障:循环重定向与混合内容排查工具地址:https://www.speedce.com 社区论坛:https://bbs.speedce.com 联系:speedceadsgmail.com写在前面 301 配成循环、http 和 https 指向不同机器——对照测快速缩小范围。 本…

2026/9/28 3:39:53 阅读更多 →
Kubernetes Ingress 故障:集群内正常、公网域名红的排查

Kubernetes Ingress 故障:集群内正常、公网域名红的排查

Kubernetes Ingress 故障:集群内正常、公网域名红的排查工具地址:https://www.speedce.com 社区论坛:https://bbs.speedce.com 联系:speedceadsgmail.com写在前面 kubectl get pods 全 Running,但公网域名红——Ingres…

2026/9/28 3:39:53 阅读更多 →

最新新闻

大模型与AI质量保障:TaoToken统一API接入主流模型全景配置指南

大模型与AI质量保障:TaoToken统一API接入主流模型全景配置指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 4:26:20 阅读更多 →
从政策到实践:一人公司如何用 AI Agent 工具链跑通运营闭环(TaoToken 配置篇)

从政策到实践:一人公司如何用 AI Agent 工具链跑通运营闭环(TaoToken 配置篇)

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 4:26:20 阅读更多 →
DeepSeek Harness 初探:一切皆插件的 Agent 框架,TaoToken 统一 Key 接入配置骨架

DeepSeek Harness 初探:一切皆插件的 Agent 框架,TaoToken 统一 Key 接入配置骨架

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 4:26:20 阅读更多 →
OpenAI 函数调用完全指南:用 TaoToken 统一 Key 打通 Function Calling 配置链路

OpenAI 函数调用完全指南:用 TaoToken 统一 Key 打通 Function Calling 配置链路

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 4:26:20 阅读更多 →
阿里云服务器可以用来干什么?搞定性能优化避坑指南

阿里云服务器可以用来干什么?搞定性能优化避坑指南

阿里云服务器可以用来干什么?搞定性能优化避坑指南 别被那些花里胡哨的模板网站骗了, 模板网站太丑不够用 ,更别提动不动就卡死、加载慢得像蜗牛。很多老板和开发者刚接触云服务器,第一反应就是“阿里云服务器可以用来干什么?”,其实这玩意儿远不止挂…

2026/9/28 4:26:20 阅读更多 →
Agent Harness 系统工程:用 TaoToken 统一 Key 打通智能体工具链配置

Agent Harness 系统工程:用 TaoToken 统一 Key 打通智能体工具链配置

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/28 4:25:20 阅读更多 →

日新闻

济南做网站多少钱:3个案例拆解,防黑源码下载全攻略

济南做网站多少钱:3个案例拆解,防黑源码下载全攻略

济南做网站多少钱:3个案例拆解,防黑源码下载全攻略 上周济南一个做建材的老板找我,脸都绿了。他的官网首页弹出了赌博广告,后台被植入了挖矿脚本。他慌得问我:“网站被黑挂马不知道怎么办?能不能直接找之前的外包公司要源码下载,看看哪里被动了手脚?…

2026/9/28 0:00:34 阅读更多 →
婚恋网站实战案例:避开3个高价坑,省钱50%还能跑赢流量

婚恋网站实战案例:避开3个高价坑,省钱50%还能跑赢流量

婚恋网站实战案例:避开3个高价坑,省钱50%还能跑赢流量 找婚恋网站建站公司,最怕的就是被坑高价。很多同行跟我吐槽,报价单上写得模棱两可,功能栏里全是“高级定制”、“专属UI”,结果落地全是套壳。今天不聊虚的,直接甩几个我经手的 实战案例…

2026/9/28 0:00:34 阅读更多 →
制作网页比较方便的软件怎么选?一文搞懂避坑指南

制作网页比较方便的软件怎么选?一文搞懂避坑指南

制作网页比较方便的软件怎么选?一文搞懂避坑指南 很多老板一上来就问:做个网站多少钱?但我反问他:你的域名买了吗?服务器租了吗?他一脸懵。这就是典型的“域名服务器搞不懂”。别急,今天咱们不聊虚的,直接 一文搞懂 那些让你头秃的技术名词。…

2026/9/28 0:00:34 阅读更多 →

周新闻

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解 【免费下载链接】spirula-studio Cross-vendor 3D Gaussian Splatting trainer - video to splat to mesh, Vulkan or CUDA. 项目地址: https://gitcode.com/GitHub_Trending/sp/spirula-studio Sp…

2026/9/27 0:00:34 阅读更多 →
SEO怎么推广速查手册新手避坑实战指南

SEO怎么推广速查手册新手避坑实战指南

SEO怎么推广速查手册新手避坑实战指南 模板网站太丑不够用?别急着加滤镜,那是治标不治本。很多老板盯着后台流量掉得眼红,却还在纠结首页Banner的圆角是不是3像素。这就像穿着西装去挖土,姿势不对,努力白费。我整理这份 速查手册…

2026/9/27 0:00:34 阅读更多 →
FireRed-OpenStoryline少样本仿写深度解析:AI Agent如何复刻你的独特文案风格与节奏

FireRed-OpenStoryline少样本仿写深度解析:AI Agent如何复刻你的独特文案风格与节奏

FireRed-OpenStoryline少样本仿写深度解析:AI Agent如何复刻你的独特文案风格与节奏 【免费下载链接】FireRed-OpenStoryline FireRed-OpenStoryline is an AI video editing agent that transforms manual editing into intention-driven directing through natural language …

2026/9/27 0:00:34 阅读更多 →

月新闻

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能

持续集成 流水线自动化与 声明式交付 实践:原型怎样变成可用功能分类:[AI/大模型]细分主题:AI 增强型 CI/CD 流水线自动化与 GitOps 实践:Agent 工作流、工具调用与任务拆解:从原型到生产的验收清单很多团队在尝试用大…

2026/9/27 9:12:14 阅读更多 →
容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场

容器编排 生产环境运维与排障实战:复盘记录怎样真正派上用场分类:[工程技术]细分主题:Kubernetes 生产环境运维与排障实战:可复制的项目复盘模板与决策记录大部分团队的事故复盘报告,最后都变成了躺在 Confluence 或钉…

2026/9/28 3:51:11 阅读更多 →
容器 容器化技术与镜像安全管理:核心链路应该先拆哪一步

容器 容器化技术与镜像安全管理:核心链路应该先拆哪一步

容器 容器化技术与镜像安全管理:核心链路应该先拆哪一步分类:[工程技术]细分主题:Docker 容器化技术与镜像安全管理:核心链路的逐步实现与关键代码取舍面对一个积累了五六年历史包袱的单体架构应用(包含 Web 接口、后台…

2026/9/26 22:52:30 阅读更多 →