【C++】类和对象——const修饰成员函数和取地址操作符重载
在上篇博客中,我们已经对于日期类有了较为全面的实现,但是,还有一个问题,比如说,我给一个const修饰的日期类的对象
这个对象是不能调用我们上篇博客写的函数的,因为&d1是const Date*类型的,而this指针是Date*类型,&d1传给this是一种权限的放大,这是不行的,所以,我们要改造一下相关函数,就是声明和定义都要加上const,那么具体形式如下
不只是这一个函数,像比较大小,加减天数等,凡是不改变this指针指向的内容的值的,都要加const,那么<<这个符号加const吗?不用,因为这不是成员函数,没有this指针
关于日期类的所有代码我会放在这篇文章的最后,下面我们来说最后两个类的默认成员函数,就是取地址操作符重载和const修饰的取地址操作符重载
这个默认成员函数确实没什么实际的作用,就算我不写这个函数,直接取地址也不会有任何问题,唯一的作用就是你可以选择不返回this,而返回空或一个假地址
Date.h文件
#include<iostream>
#include<assert.h>
using namespace std;
class Date {
public:Date(int year = 1, int month = 1, int day = 1);Date* operator&();const Date* operator&()const;void Print()const;int GetMonthDay(int year, int month);bool operator==(const Date& n);bool operator!=(const Date& n);bool operator<(const Date& n);bool operator<=(const Date& n);bool operator>(const Date& n);bool operator>=(const Date& n);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);int operator-(const Date& d);friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp文件
#include"Date.h"
Date::Date(int year, int month , int day ) {_year = year;_month = month;_day = day;if (_year < 1 || _month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)) {cout << _year << "年" << _month << "月" << _day << "日";cout << "日期非法" << endl;}
}
Date* Date:: operator&() {return this;
}const Date* Date::operator&()const {return this;
}void Date::Print() const{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}int Date :: GetMonthDay(int year, int month) {assert(year >= 1 && month >= 1 && month <= 12);int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month == 2) && (((year % 400) == 0) || ((year % 4) == 0 && (year % 100) != 0))) {return 29;}return monthArray[month];
}bool Date:: operator==(const Date& n) {return _year == n._year && _month == n._month && _day == n._day;
}bool Date::operator!=(const Date& n) {return !(*this == n);
}bool Date:: operator<(const Date& n) {if (_year < n._year) {return true;}if (_year == n._year && _month < n._month) {return true;}if (_year == n._year && _month == n._month && _day < n._day) {return true;}return false;
}bool Date::operator<=(const Date& n) {return ((*this < n) || (*this == n));
}bool Date::operator>(const Date& n) {return !(*this <= n);
}bool Date:: operator>=(const Date& n) {//return *this > n || *this == n;return !(*this < n);
}Date& Date:: operator+=(int day) {if (day < 0) {return *this -= (-day);}if (day < 0) {return *this -= (-day);}_day += day;while (_day > GetMonthDay(_year, _month)) {_day -= GetMonthDay(_year, _month);++_month;if (_month == 13) {++_year;_month = 1;}}return *this;
}Date Date:: operator+(int day) {Date tmp(*this);tmp += day;return tmp;
}Date& Date:: operator-=(int day) {if (day < 0) {return *this -= (-day);}if (day < 0) {return *this += (-day);}_day -= day;while (_day <= 0) {--_month;if (_month == 0) {--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date:: operator-(int day) {Date tmp(*this);tmp -= day;return tmp;
}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;
}//int Date::operator-(const Date& d) {
// int flag = -1;
// Date min = *this;
// Date max = d;
// if (*this > d) {
// min = d;
// max = *this;
// flag = 1;
// }
// int n = 0;
// while (min < max) {
// ++min;
// ++n;
// }
// return n*flag;
//}
int Date::operator-(const Date& d) {int flag = 1;Date max = *this;Date min = d;if (*this < d) {max = d;min = *this;flag = -1;}int n = 0;int y = min._year;while (y != max._year) {if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {n += 366;}else {n += 365;}y++;}int m1 = 1;int m2 = 1;while (m1 < max._month) {n += GetMonthDay(max._year, m1);m1++;}while (m2 < min._month) {n -= GetMonthDay(min._year, m2);m2++;}n = n + max._day - min._day;return n;
}
ostream& operator<<(ostream& out, const Date& d) {out << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Date& d) {in >> d._year>>d._month >> d._day;return in;
}
相关文章:

【C++】类和对象——const修饰成员函数和取地址操作符重载
在上篇博客中,我们已经对于日期类有了较为全面的实现,但是,还有一个问题,比如说,我给一个const修饰的日期类的对象 这个对象是不能调用我们上篇博客写的函数的,因为&d1是const Date*类型的ÿ…...

express+mySql实现用户注册、登录和身份认证
expressmySql实现用户注册、登录和身份认证 注册 注册时需要对用户密码进行加密入库,提高账户的安全性。用户登录时再将密码以相同的方式进行加密,再与数据库中存储的密码进行比对,相同则表示登录成功。 安装加密依赖包bcryptjs cnpm insta…...
【PyTorch】(二)加载数据集
文章目录 1. 创建数据集1.1. 直接继承Dataset类1.2. 使用TensorDataset类 2. 加载数据集3. 将数据转移到GPU 1. 创建数据集 主要是将数据集读入内存,并用Dataset类封装。 1.1. 直接继承Dataset类 必须要重写__getitem__方法,用于根据索引获得相应样本…...

如何提高3D建模技能?
无论是制作影视动画还是视频游戏,提高3D建模技能对于你的工作都至关重要的。那么如何能创建出精美的3D模型呢?本文给大家一些3D建模技能方面的建议。 3D建模通过专门的软件完成,涉及制作三维对象。这项技能在视频游戏开发、建筑、动画和产品…...

【前端开发】Next.js与Nest.js之间的差异2023
在快节奏的网络开发领域,JavaScript已成为构建可靠且引人入胜的在线应用程序的标准语言。然而,随着对适应性强、高效的在线服务的需求不断增加,开发人员通常不得不从广泛的库和框架中进行选择,以满足其项目的要求。Next.js和Nest.…...

【CAN通信】CanIf模块详细介绍
目录 1.内容简介 2.CanIf详细设计 2.1 CanIf功能简介 2.2 一些关键概念 2.3依赖的上下层模块 2.4 功能详细设计 2.4.1 Hardware object handles 2.4.2 Static L-PDUs 2.4.3 Dynamic L-PDUs 2.4.4 Dynamic Transmit L-PDUs 2.4.5 Dynamic receive L-PDUs 2.4.6Physi…...

PS最新磨皮软件Portraiture4.1.2
Portraiture是一款好用的PS磨皮滤镜插件,拥有磨皮美白的功能,操作也很简单,一键点击即可实现美白效果,软件还保留了人物的皮肤质感让照片看起来更加真实。portraiture体积小巧,不会占用过多的电脑内存哦。 内置了多种…...

旋转框(obb)目标检测计算iou的方法
首先先定义一组多边形,这里的数据来自前后帧的检测结果 pre [[[860.0, 374.0], [823.38, 435.23], [716.38, 371.23], [753.0, 310.0]],[[829.0, 465.0], [826.22, 544.01], [684.0, 539.0], [686.78, 459.99]],[[885.72, 574.95], [891.0, 648.0], [725.0, 660.0]…...
render函数举例
在这段代码中,renderButton是一个对象吗 还有render为什么不能写成render() {} 代码原文链接 <template><div><renderButton /></div> </template><script setup> import { h, ref } from "vue"; const renderButt…...

微信小程序文件预览和下载-文件系统
文件预览和下载 在下载之前,我们得先调用接口获取文件下载的url 然后通过wx.downloadFile将下载文件资源到本地 wx.downloadFile({url: res.data.url,success: function (res) {console.log(数据,res);} })tempFilePath就是临时临时文件路径。 通过wx.openDocume…...

图解Redis适用场景
Redis以其速度而闻名。 1 业务数据缓存 1.1 通用数据缓存 string,int,list,map。Redis 最常见的用例是缓存对象以加速 Web 应用程序。 此用例中,Redis 将频繁请求的数据存储在内存。允许 Web 服务器快速返回频繁访问的数据。这…...

掌握Python BentoML:构建、部署和管理机器学习模型
更多资料获取 📚 个人网站:ipengtao.com BentoML是一个开源的Python框架,旨在简化机器学习模型的打包、部署和管理。本文将深入介绍BentoML的功能和用法,提供详细的示例代码和解释,帮助你更好地理解和应用这个强大的工…...

西南科技大学模拟电子技术实验二(二极管特性测试及其应用电路)预习报告
目录 一、计算/设计过程 二、画出并填写实验指导书上的预表 三、画出并填写实验指导书上的虚表 四、粘贴原理仿真、工程仿真截图 一、计算/设计过程 说明:本实验是验证性实验,计算预测验证结果。是设计性实验一定要从系统指标计算出元件参数过程,越详细越好。用公式输入…...

熟悉SVN基本操作-(SVN相关介绍使用以及冲突解决)
一、SVN相关介绍 1、SVN是什么? 代码版本管理工具它能记住你每次的修改查看所有的修改记录恢复到任何历史版本恢复已经删除的文件 2、SVN跟Git比,有什么优势 使用简单,上手快目录级权限控制,企业安全必备子目录checkout,减少…...
代码随想录二刷 |字符串 |反转字符串II
代码随想录二刷 |字符串 |反转字符串II 题目描述解题思路 & 代码实现 题目描述 541.反转字符串II 给定一个字符串 s 和一个整数 k,从字符串开头算起,每计数至 2k 个字符,就反转这 2k 字符中的前 k 个字符。 如果…...

哪吒汽车拔头筹,造车新势力首家泰国工厂投产
中国造车新势力首家泰国工厂投产!11月30日,哪吒汽车位于泰国的首家海外工厂——泰国生态智慧工厂正式投产下线新车,哪吒汽车联合创始人兼CEO张勇、哪吒汽车泰国合作伙伴BGAC公司首席执行官万查曾颂翁蓬素等出席仪式。首辆“泰国制造”的哪吒汽…...

Redis String类型
String 类型是 Redis 最基本的数据类型,String 类型在 Redis 内部使用动态长度数组实现,Redis 在存储数据时会根据数据的大小动态地调整数组的长度。Redis 中字符串类型的值最大可以达到 512 MB。 关于字符串需要特别注意∶ 首先,Redis 中所…...

lxd提权
lxd/lxc提权 漏洞介绍 lxd是一个root进程,它可以负责执行任意用户的lxd,unix套接字写入访问操作。而且在一些情况下,lxd不会调用它的用户权限进行检查和匹配 原理可以理解为用用户创建一个容器,再用容器挂载宿主机磁盘…...

Ubuntu+Tesla V100环境配置
系统基本信息 nvidia-smi’ nvidia-smi 470.182.03 driver version:470.182.03 cuda version: 11.4 查看系统体系结构 uname -aUTC 2023 x86_64 x86_64 x86_64 GNU/Linux 下载miniconda https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/?CM&OA https://mi…...

leetcode:用栈实现队列(先进先出)
题目描述 题目链接:232. 用栈实现队列 - 力扣(LeetCode) 题目分析 我们先把之前写的数组栈的实现代码搬过来 用栈实现队列最主要的是实现队列先进先出的特点,而栈的特点是后进先出,那么我们可以用两个栈来实现&…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

【JavaEE】-- HTTP
1. HTTP是什么? HTTP(全称为"超文本传输协议")是一种应用非常广泛的应用层协议,HTTP是基于TCP协议的一种应用层协议。 应用层协议:是计算机网络协议栈中最高层的协议,它定义了运行在不同主机上…...

Unity3D中Gfx.WaitForPresent优化方案
前言 在Unity中,Gfx.WaitForPresent占用CPU过高通常表示主线程在等待GPU完成渲染(即CPU被阻塞),这表明存在GPU瓶颈或垂直同步/帧率设置问题。以下是系统的优化方案: 对惹,这里有一个游戏开发交流小组&…...

黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门 
C++ Visual Studio 2017厂商给的源码没有.sln文件 易兆微芯片下载工具加开机动画下载。
1.先用Visual Studio 2017打开Yichip YC31xx loader.vcxproj,再用Visual Studio 2022打开。再保侟就有.sln文件了。 易兆微芯片下载工具加开机动画下载 ExtraDownloadFile1Info.\logo.bin|0|0|10D2000|0 MFC应用兼容CMD 在BOOL CYichipYC31xxloaderDlg::OnIni…...

推荐 github 项目:GeminiImageApp(图片生成方向,可以做一定的素材)
推荐 github 项目:GeminiImageApp(图片生成方向,可以做一定的素材) 这个项目能干嘛? 使用 gemini 2.0 的 api 和 google 其他的 api 来做衍生处理 简化和优化了文生图和图生图的行为(我的最主要) 并且有一些目标检测和切割(我用不到) 视频和 imagefx 因为没 a…...

【Linux手册】探秘系统世界:从用户交互到硬件底层的全链路工作之旅
目录 前言 操作系统与驱动程序 是什么,为什么 怎么做 system call 用户操作接口 总结 前言 日常生活中,我们在使用电子设备时,我们所输入执行的每一条指令最终大多都会作用到硬件上,比如下载一款软件最终会下载到硬盘上&am…...
高防服务器价格高原因分析
高防服务器的价格较高,主要是由于其特殊的防御机制、硬件配置、运营维护等多方面的综合成本。以下从技术、资源和服务三个维度详细解析高防服务器昂贵的原因: 一、硬件与技术投入 大带宽需求 DDoS攻击通过占用大量带宽资源瘫痪目标服务器,因此…...

Xcode 16 集成 cocoapods 报错
基于 Xcode 16 新建工程项目,集成 cocoapods 执行 pod init 报错 ### Error RuntimeError - PBXGroup attempted to initialize an object with unknown ISA PBXFileSystemSynchronizedRootGroup from attributes: {"isa">"PBXFileSystemSynchro…...
游戏开发中常见的战斗数值英文缩写对照表
游戏开发中常见的战斗数值英文缩写对照表 基础属性(Basic Attributes) 缩写英文全称中文释义常见使用场景HPHit Points / Health Points生命值角色生存状态MPMana Points / Magic Points魔法值技能释放资源SPStamina Points体力值动作消耗资源APAction…...