c++学习第七天
创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。
提示:以下是本篇文章正文内容,下面案例可供参考。
一、const成员函数

//Date.h#pragma once#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1);//两个Print()可以同时存在,构成函数重载void Print() const;void Print();private:int _year;int _month;int _day;
};
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}//void Date::Print(const Date* this)
void Date::Print() const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}//void Date::Print(Date* this)
void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}
//Test.cpp#include"Date.h"int main()
{const Date d1(2025, 1, 1);d1.Print();Date d2(2025, 1, 2);d2.Print();
}

//Date.hbool operator<(const Date& d) const;bool operator==(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;bool operator!=(const Date& d) const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);Date operator-(int day) const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int operator-(const Date& d) const;
简单来说:只读函数可以加const,内部不涉及修改成员的都是只读函数
二、取地址及const取地址操作符重载
//Test.cpp#include"Date.h"int main()
{const Date d1(2025, 1, 2);d1.Print();cout << &d1 << endl;Date d2(2025, 1, 7);d2.Print();cout << &d2 << endl;return 0;
}

理论上上面的代码是不能运行的,因为自定义类型使用运算符需要重载。因为取地址及const取地址操作符重载这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
//自己写如下:
//日常使用自动生成的就可以了Date* operator&()
{return this;
}const Date* operator&() const
{return this;
}
// 不想被取到有效地址才自己写
Date* operator&()
{return nullptr;
}const Date* operator&() const
{return this;
}

三、流插入和流提取
//Date.hvoid operator<<(ostream& out);
//Date.cppvoid Date::operator<<(ostream& out)
{out << _year << "/" << _month << "/" << _day << endl;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);cout << d1 ;return 0;
}

原因:参数顺序不匹配
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);d1 << cout ;return 0;
}
![]()
不能写成成员函数,因为成员函数this指针永远占据第一个,所以只能往全局上写。
//Date.h#pragma once#include<iostream>
using namespace std;class Date
{// 友元声明//因为operator<<写在函数外面不能访问到_year,_month,_dayfriend void operator<<(ostream& out, const Date& d);Date(int year = 1, int month = 1, int day = 1);public:void Print();private:int _year;int _month;int _day;
};void operator<<(ostream& out, const Date& d);
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}void operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);Date d2(2024, 2, 2);cout << d1 ;return 0;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);Date d2(2024, 2, 2);cout << d1 << d2 ;return 0;
}

//Date.h#pragma once#include<iostream>
using namespace std;class Date
{friend ostream& operator<<(ostream& out, const Date& d);Date(int year = 1, int month = 1, int day = 1);public:void Print();private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);
//Date.cpp#include"Date.h"Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;// 检查日期是否合法if (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._day << endl;return out;
}
//Date.h#pragma once#include<iostream>
using namespace std;class Date
{// 友元声明friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);Date(int year = 1, int month = 1, int day = 1);public:void Print();private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);//这里不能加const,因为这是在内部修改让外部拿到这个值
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 (month < 1 || month > 12|| day < 1 || day > GetMonthDay(year, month)){cout << "非法日期" << endl;// exit(-1);}
}void Date::Print()
{cout << _year << "年" << _month << "月" << endl;
}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;
}
//Test.cpp#include"Date.h"int main()
{Date d1(2025, 1, 21);Date d2(2024, 2, 2);cout << d1 << d2;cin >> d1;cout << d1;return 0;
}

四、再谈构造函数
1.初始化列表
class Date
{
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}private:int _year;int _month;int _day;
};
class A
{
public:A(int a = 0):_a(a){}
private:int _a;
};class Date
{
public:// 初始化列表是每个成员定义的地方// 不管你写不写,每个成员都要走初始化列表Date(int year, int month, int day, int& i): _year(year), _month(month),_a(1),_refi(i){// 赋值_day = day;}private:int _year; // 每个成员声明int _month = 1 ;int _day = 1;// C++11支持给缺省值,这个缺省值给初始化列表// 如果初始化列表没有显示给值,就用这个缺省值// 如果显示给值了,就不用这个缺省值//这里_day会使用缺省值,而_month不会使用// 下面三个例子必须定义时初始化const int _x = 10;int& _refi;A _a;
};
注意:
1. 每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)。
2.类中包含以下成员,必须放在初始化列表位置进行初始化:
①引用成员变量 ②const成员变量 ③自定义类型成员(且该类没有默认构造函数时)
4.成员变量在类中声明次序就是其在初始化列表中的初始化顺序,与其在初始化列表中的先后
总结:
能用初始化列表就用初始化初始化列表,但有些场景还是需要初始化列表和函数体混着用。
五、explicit关键字
#include<iostream>
using namespace std;class A
{
public:A(int i)//A(int i):_a(i){cout << "A(int i)" << endl;}private:int _a;
};int main()
{A aa1(1);// 单参数构造函数的隐式类型转换// 用2调用A构造函数生成一个临时对象,再用这个对象去拷贝构造aa2// 编译器会再优化,优化用2直接构造 A aa2 = 2;return 0;
}

A& ref1 = 2;
![]()
这里的问题原因是临时对象具有常性,ref不能引用,属于权限的放大,加上const就没有问题了。
const A& ref1 = 2;
#include<iostream>
using namespace std;class B
{
public:B(int b1, int b2)//explicit B(int b1, int b2):_b1(b1), _b2(b2){cout << "B(int b1, int b2)" << endl;}
private:int _b1;int _b2;
};int main()
{// C++11 支持多参数的隐式类型转换B bb1(1, 1);B bb2 = { 2, 2 };const B& ref2 = { 3,3 };return 0;
}

class B
{
public:explicit B(int b1, int b2):_b1(b1), _b2(b2){cout << "B(int b1, int b2)" << endl;}
private:int _b1;int _b2;
};
重点:
class A
{
public:explicit A(int i):_a(i){cout << "A(int i)" << endl;}private:int _a;
};class B
{
public:explicit B(int b1, int b2):_b1(b1), _b2(b2){cout << "B(int b1, int b2)" << endl;}
private:int _b1;int _b2;
}; 六、生命周期
#include<iostream>
using namespace std;class A
{
public:explicit A(int i):_a(i){cout << "A(int i)" << endl;}A(const A& aa):_a(aa._a){cout << "A(const A& aa)" << endl;}~A(){cout << "~A()" << _a << endl;}
private:int _a;
};int main()
{// 有名对象 特点:生命周期在当前局部域A aa6(6);// 匿名对象。特点:生命周期只在这一行A(7);return 0;
}

总结
以上就是今天要讲的内容,本文仅仅简单介绍了c++的基础。
相关文章:
c++学习第七天
创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。 提示:以下是本篇文章正文内容,下面案例可供参考。 一、const成员函数 //Date.h#pragma once#include<iostream> using namespace std;class Date { public:Date…...
Ubuntu 24.04 LTS 通过 docker 安装 nextcloud 搭建个人网盘
准备 Ubuntu 24.04 LTSUbuntu 空闲硬盘挂载Ubuntu 安装 Docker DesktopUbuntu 24.04 LTS 安装 tailscale [我的Ubuntu服务器折腾集](https://blog.csdn.net/jh1513/article/details/145222679。 安装 nextcloud 参考 Ubuntu24.04系统Docker安装NextcloudOnlyoffice _。 更…...
RabbitMQ1-消息队列
目录 MQ的相关概念 什么是MQ 为什么要用MQ MQ的分类 MQ的选择 RabbitMQ RabbitMQ的概念 四大核心概念 RabbitMQ的核心部分 各个名词介绍 MQ的相关概念 什么是MQ MQ(message queue),从字面意思上看,本质是个队列,FIFO 先入先出&am…...
Open3D计算点云粗糙度(方法一)【2025最新版】
目录 一、Roughness二、代码实现三、结果展示博客长期更新,本文最近更新时间为:2025年1月18日。 一、Roughness 通过菜单栏的Tools > Other > Roughness找到该功能。 这个工具可以估计点云的“粗糙度”。 选择一个或几个点云,然后启动这个工具。 CloudCompare只会询问…...
算法6(力扣148)-排序链表
1、问题 给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 2、采用例子 输入:head [4,2,1,3] 输出:[1,2,3,4] 3、实现思路 将链表拆分成节点,存入数组使用sort排序,再用reduce重建链接 4、具…...
一文大白话讲清楚webpack基本使用——9——预加载之prefetch和preload以及webpackChunkName的使用
文章目录 一文大白话讲清楚webpack基本使用——9——预加载之prefetch和preload1. 建议按文章顺序从头看,一看到底,豁然开朗2. preload和prefetch的区别2. prefetch的使用3. preload的使用4. webpackChunkName 一文大白话讲清楚webpack基本使用——9——…...
【大数据2025】MapReduce
MapReduce 基础介绍 起源与发展:是 2004 年 10 月谷歌发表的 MAPREDUCE 论文的开源实现,最初用于大规模网页数据并行处理,现成为 Hadoop 核心子项目之一,是面向批处理的分布式计算框架。基本原理:分为 map 和 reduce …...
Windows图形界面(GUI)-QT-C/C++ - Qt List Widget详解与应用
公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 QListWidget概述 使用场景 常见样式 QListWidget属性设置 显示方式 (Display) 交互行为 (Interaction) 高级功能 (Advanced) QListWidget常见操作 内容处理 增加项目 删除项目…...
深度学习python基础(第二节) 分支语句和循环语句
本节主要介绍分支语句和循环语句的基本语法。 注意:在python中的作用域以缩进为准。有语言基础的很好理解,了解语法格式就可以。 布尔类型和比较运算符 # True真,False假 a True print(f"布尔变量a的内容是:{a},类型是:{type(a)}") 比较运算…...
Gin 源码概览 - 路由
本文基于gin 1.1 源码解读 https://github.com/gin-gonic/gin/archive/refs/tags/v1.1.zip 1. 注册路由 我们先来看一段gin代码,来看看最终得到的一颗路由树长啥样 func TestGinDocExp(t *testing.T) {engine : gin.Default()engine.GET("/api/user", f…...
第6章 ThreadGroup详细讲解(Java高并发编程详解:多线程与系统设计)
1.ThreadGroup 与 Thread 在Java程序中, 默认情况下, 新的线程都会被加入到main线程所在的group中, main线程的group名字同线程名。如同线程存在父子关系一样, Thread Group同样也存在父子关系。图6-1就很好地说明了父子thread、父…...
CentOS 7乱码问题如何解决?
1.使用超级用户操作: sudo su2.修改i18n配置文件: vi /etc/sysconfig/i18n将文件修改或添加为以下内容: LANG"zh_CN.UTF8" LC_ALL"zh_CN.UTF8"保存并退出(按Esc键,输入:wq,然后回车)…...
JavaScript语言的多线程编程
JavaScript语言的多线程编程 JavaScript是一种广泛使用的编程语言,主要用于网页开发。由于其单线程的特性,JavaScript 一直以来都有“无法进行多线程编程”的印象。尽管如此,随着技术的发展,JavaScript也逐渐引入了多线程的概念&…...
OpenSeaOtter使用手册-变更通知和持续部署
我们在OpenSeaOtter Server 0.1.1版本增加的镜像变更通知功能。通过镜像变更通知和OpenSeaOtter Agent就可以轻松获得持续部署能力。 镜像变更通知是通过push的方式下发到Agent的,Agent所在机器不需要外网地址。在Agent收到镜像变更通知后,就会调用对应的…...
(2)STM32 USB设备开发-USB虚拟串口
例程:STM32USBdevice: 基于STM32的USB设备例子程序 - Gitee.com 本篇为USB虚拟串口教程,没有知识,全是实操,按照步骤就能获得一个STM32的USB虚拟串口。本例子是在野火F103MINI开发板上验证的,如果代码中出现一些外设的…...
他把智能科技引入现代农业领域
江苏田倍丰农业科技有限公司(以下简称“田倍丰”)是一家专注于粮油种植的农业科技公司,为拥有300亩以上田地的大户提供全面的解决方案。田倍丰通过与当地政府合作,将土地承包给大户,并提供农资和技术,实现利…...
ingress-nginx代理tcp使其能外部访问mysql
一、helm部署mysql主从复制 helm repo add bitnami https://charts.bitnami.com/bitnami helm repo updatehelm pull bitnami/mysql 解压后编辑values.yaml文件,修改如下(storageclass已设置默认类) 117 ## param architecture MySQL archit…...
麒麟操作系统服务架构保姆级教程(十三)tomcat环境安装以及LNMT架构
如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情 之前咱们学习了LNMP架构,但是PHP对于技术来说确实是老掉牙了,PHP的市场占有量越来越少了,我认识一个10年的PHP开发工程师,十年工资从15k到今天的6k&am…...
亚博microros小车-原生ubuntu支持系列:4-手部检测
一 准备工作 在学习手部检测之前,有2个准备工作。 1 确保小车的摄像头能显示画面 参见:亚博microros小车-原生ubuntu支持系列:2-摄像头控制-CSDN博客 启动图传代理: docker run -it --rm -v /dev:/dev -v /dev/shm:/dev/shm …...
关于回调函数(callback)
简介 在C中,回调函数是一种常见的编程技术,它允许你将一个函数作为参数传递给另一个函数,并在适当的时候调用它。回调函数通常用于事件处理、异步编程和模块化设计中。 1、函数指针:在C中,回调函数通常是通过函数指针…...
React Native 开发环境搭建(全平台详解)
React Native 开发环境搭建(全平台详解) 在开始使用 React Native 开发移动应用之前,正确设置开发环境是至关重要的一步。本文将为你提供一份全面的指南,涵盖 macOS 和 Windows 平台的配置步骤,如何在 Android 和 iOS…...
从零实现富文本编辑器#5-编辑器选区模型的状态结构表达
先前我们总结了浏览器选区模型的交互策略,并且实现了基本的选区操作,还调研了自绘选区的实现。那么相对的,我们还需要设计编辑器的选区表达,也可以称为模型选区。编辑器中应用变更时的操作范围,就是以模型选区为基准来…...
智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql
智慧工地管理云平台系统,智慧工地全套源码,java版智慧工地源码,支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求,提供“平台网络终端”的整体解决方案,提供劳务管理、视频管理、智能监测、绿色施工、安全管…...
3.3.1_1 检错编码(奇偶校验码)
从这节课开始,我们会探讨数据链路层的差错控制功能,差错控制功能的主要目标是要发现并且解决一个帧内部的位错误,我们需要使用特殊的编码技术去发现帧内部的位错误,当我们发现位错误之后,通常来说有两种解决方案。第一…...
PL0语法,分析器实现!
简介 PL/0 是一种简单的编程语言,通常用于教学编译原理。它的语法结构清晰,功能包括常量定义、变量声明、过程(子程序)定义以及基本的控制结构(如条件语句和循环语句)。 PL/0 语法规范 PL/0 是一种教学用的小型编程语言,由 Niklaus Wirth 设计,用于展示编译原理的核…...
全面解析各类VPN技术:GRE、IPsec、L2TP、SSL与MPLS VPN对比
目录 引言 VPN技术概述 GRE VPN 3.1 GRE封装结构 3.2 GRE的应用场景 GRE over IPsec 4.1 GRE over IPsec封装结构 4.2 为什么使用GRE over IPsec? IPsec VPN 5.1 IPsec传输模式(Transport Mode) 5.2 IPsec隧道模式(Tunne…...
【Oracle】分区表
个人主页:Guiat 归属专栏:Oracle 文章目录 1. 分区表基础概述1.1 分区表的概念与优势1.2 分区类型概览1.3 分区表的工作原理 2. 范围分区 (RANGE Partitioning)2.1 基础范围分区2.1.1 按日期范围分区2.1.2 按数值范围分区 2.2 间隔分区 (INTERVAL Partit…...
蓝桥杯 冶炼金属
原题目链接 🔧 冶炼金属转换率推测题解 📜 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V,是一个正整数,表示每 V V V 个普通金属 O O O 可以冶炼出 …...
现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?
现有的 Redis 分布式锁库(如 Redisson)相比于开发者自己基于 Redis 命令(如 SETNX, EXPIRE, DEL)手动实现分布式锁,提供了巨大的便利性和健壮性。主要体现在以下几个方面: 原子性保证 (Atomicity)ÿ…...
GitFlow 工作模式(详解)
今天再学项目的过程中遇到使用gitflow模式管理代码,因此进行学习并且发布关于gitflow的一些思考 Git与GitFlow模式 我们在写代码的时候通常会进行网上保存,无论是github还是gittee,都是一种基于git去保存代码的形式,这样保存代码…...
