news 2026/9/3 4:30:54

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++ 类的默认成员函数详解:构造、析构与拷贝构造
  • 第一:我们不写时,编译器默认生成的函数行为是什么,是否满足我们的需求
  • 编译器默认生成的函数不满足我们的需求,我们需要自己实现,那么如何自己实现?

在这里插入图片描述

二、构造函数

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象(我们常使用的局部对象是栈帧创建时,空间就开好了),而是对象实例化时初始化对象。构造函数的本质是要替代之前Stack和Date类中写的Init函数功能,构造函数自动调用的特点就完美的替代了Init。

构造函数的特点

  1. 函数名与类名相同
  2. 无返回值(返回值啥都不需要给,也不需要写void)
  3. 对象实例化时系统会自动调用对应的构造函数(构造函数可以有多个)
  4. 构造函数可以重载,构造函数可能有多个,一个类可能需要多种初始化方式。比如说链表可以初始化一个空的链表出来,也可以用一个固定的值去初始化

在这里插入图片描述

C++中用构造函数替代Init函数的原因是,Init函数其对象的定义和初始化是分离的,有时候可能会忘记初始化,C++这个机制是为了保证对象定义实例化出来就一定初始化了

代码语言:javascript

AI代码解释

Date d1; d1.Init(&d1, 2025, 9, 24);
  1. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成
  2. 无参构造函数(1)、全缺省构造函数(2)、我们不写构造时编译器默认生成的构造函数(3),都叫做默认构造函数(特点就是不传实参就可以调用)。但是这三个函数有且只有一个存在,不能同时存在。无参构造函数和全缺省构造函数虽然构成函数重载,但是调用时会存在歧义(不知道不传参的时候调用谁)。要注意很多人会认为默认构造函数是编译器默认生成的那个叫默认构造,实际上无参构造函数、全缺省构造函数也是默认构造,总结一下就是不传实参就可以调用的构造就叫默认构造。

在这里插入图片描述

  1. 我们不写,编译器默认生成的构造,对内置类型成员变量的初始化没有要求

在这里插入图片描述

也就是说是否初始化是不确定的,看似默认构造什么都没干,看编译器。 对于自定义类型成员变量,要求调用这个成员变量的默认构造函数初始化。如果这个成员变量,没有默认构造函数,那么就会报错,我们要初始化这个成员变量,需要用初始化列表才能解决,初始化列表,我的下一篇文章会写

map.xrezeku.cn/Blog/199513.shtml
map.xrezeku.cn/Blog/519379.shtml
map.xrezeku.cn/Blog/935933.shtml
map.xrezeku.cn/Blog/644682.shtml
map.xrezeku.cn/Blog/804468.shtml
map.xrezeku.cn/Blog/082248.shtml
map.xrezeku.cn/Blog/446062.shtml
map.xrezeku.cn/Blog/953115.shtml
map.xrezeku.cn/Blog/339915.shtml
map.xrezeku.cn/Blog/791171.shtml
map.xrezeku.cn/Blog/593173.shtml
map.xrezeku.cn/Blog/737557.shtml
map.xrezeku.cn/Blog/933333.shtml
map.xrezeku.cn/Blog/111153.shtml
map.xrezeku.cn/Blog/717137.shtml
map.xrezeku.cn/Blog/155313.shtml
map.xrezeku.cn/Blog/315177.shtml
map.xrezeku.cn/Blog/197739.shtml
map.xrezeku.cn/Blog/139315.shtml
map.xrezeku.cn/Blog/391973.shtml
map.xrezeku.cn/Blog/959553.shtml
map.xrezeku.cn/Blog/777335.shtml
map.xrezeku.cn/Blog/337313.shtml
map.xrezeku.cn/Blog/448206.shtml
map.xrezeku.cn/Blog/064622.shtml
map.xrezeku.cn/Blog/999317.shtml
map.xrezeku.cn/Blog/482868.shtml
map.xrezeku.cn/Blog/537735.shtml
map.xrezeku.cn/Blog/939999.shtml
map.xrezeku.cn/Blog/775357.shtml
map.xrezeku.cn/Blog/733135.shtml
map.xrezeku.cn/Blog/804064.shtml
map.xrezeku.cn/Blog/995399.shtml
map.xrezeku.cn/Blog/711164.shtml
map.xrezeku.cn/Blog/842602.shtml
map.xrezeku.cn/Blog/937779.shtml
map.xrezeku.cn/Blog/628840.shtml
map.xrezeku.cn/Blog/229899.shtml
map.xrezeku.cn/Blog/802240.shtml
map.xrezeku.cn/Blog/319375.shtml
map.xrezeku.cn/Blog/206002.shtml
map.xrezeku.cn/Blog/315979.shtml
map.xrezeku.cn/Blog/591939.shtml
map.xrezeku.cn/Blog/220620.shtml
map.xrezeku.cn/Blog/353573.shtml
map.xrezeku.cn/Blog/197719.shtml
map.xrezeku.cn/Blog/537111.shtml
map.xrezeku.cn/Blog/244222.shtml
map.xrezeku.cn/Blog/915153.shtml
map.xrezeku.cn/Blog/155973.shtml
map.xrezeku.cn/Blog/428088.shtml
map.xrezeku.cn/Blog/133957.shtml
map.xrezeku.cn/Blog/751155.shtml
map.xrezeku.cn/Blog/204622.shtml
map.xrezeku.cn/Blog/397351.shtml
map.xrezeku.cn/Blog/200208.shtml
map.xrezeku.cn/Blog/557333.shtml
map.xrezeku.cn/Blog/759135.shtml
map.xrezeku.cn/Blog/660480.shtml
map.xrezeku.cn/Blog/513115.shtml
map.xrezeku.cn/Blog/397359.shtml
map.xrezeku.cn/Blog/995551.shtml
map.xrezeku.cn/Blog/513393.shtml
map.xrezeku.cn/Blog/668226.shtml
map.xrezeku.cn/Blog/139133.shtml
map.xrezeku.cn/Blog/022206.shtml
map.xrezeku.cn/Blog/226028.shtml
map.xrezeku.cn/Blog/935591.shtml
map.xrezeku.cn/Blog/733995.shtml
map.xrezeku.cn/Blog/646808.shtml
map.xrezeku.cn/Blog/919157.shtml
map.xrezeku.cn/Blog/957771.shtml
map.xrezeku.cn/Blog/719571.shtml
map.xrezeku.cn/Blog/531153.shtml
map.xrezeku.cn/Blog/937737.shtml
map.xrezeku.cn/Blog/115379.shtml
map.xrezeku.cn/Blog/888202.shtml
map.xrezeku.cn/Blog/515953.shtml
map.xrezeku.cn/Blog/884404.shtml
map.xrezeku.cn/Blog/155171.shtml
map.xrezeku.cn/Blog/644486.shtml
map.xrezeku.cn/Blog/395111.shtml
map.xrezeku.cn/Blog/731131.shtml
map.xrezeku.cn/Blog/224486.shtml
map.xrezeku.cn/Blog/311131.shtml
map.xrezeku.cn/Blog/535573.shtml
map.xrezeku.cn/Blog/208240.shtml
map.xrezeku.cn/Blog/331393.shtml
map.xrezeku.cn/Blog/735955.shtml
map.xrezeku.cn/Blog/628826.shtml
map.xrezeku.cn/Blog/844046.shtml
map.xrezeku.cn/Blog/624846.shtml
map.xrezeku.cn/Blog/848480.shtml
map.xrezeku.cn/Blog/864468.shtml
map.xrezeku.cn/Blog/119755.shtml
map.xrezeku.cn/Blog/313719.shtml
map.xrezeku.cn/Blog/795375.shtml
map.xrezeku.cn/Blog/820406.shtml
map.xrezeku.cn/Blog/688622.shtml
map.xrezeku.cn/Blog/919179.shtml
map.xrezeku.cn/Blog/446800.shtml
map.xrezeku.cn/Blog/206860.shtml
map.xrezeku.cn/Blog/733373.shtml
map.xrezeku.cn/Blog/317379.shtml
map.xrezeku.cn/Blog/246482.shtml
map.xrezeku.cn/Blog/042280.shtml
map.xrezeku.cn/Blog/008062.shtml
map.xrezeku.cn/Blog/959795.shtml
map.xrezeku.cn/Blog/795993.shtml
map.xrezeku.cn/Blog/393315.shtml
map.xrezeku.cn/Blog/202048.shtml
map.xrezeku.cn/Blog/975397.shtml
map.xrezeku.cn/Blog/488226.shtml
map.xrezeku.cn/Blog/086402.shtml
map.xrezeku.cn/Blog/171111.shtml
map.xrezeku.cn/Blog/242266.shtml
map.xrezeku.cn/Blog/022088.shtml
map.xrezeku.cn/Blog/335951.shtml
map.xrezeku.cn/Blog/880444.shtml
map.xrezeku.cn/Blog/779399.shtml
map.xrezeku.cn/Blog/600862.shtml
map.xrezeku.cn/Blog/355395.shtml
map.xrezeku.cn/Blog/006844.shtml
map.xrezeku.cn/Blog/377991.shtml
map.xrezeku.cn/Blog/711917.shtml
map.xrezeku.cn/Blog/220846.shtml
map.xrezeku.cn/Blog/422800.shtml
map.xrezeku.cn/Blog/804088.shtml
map.xrezeku.cn/Blog/713975.shtml
map.xrezeku.cn/Blog/953799.shtml
map.xrezeku.cn/Blog/355351.shtml
map.xrezeku.cn/Blog/133317.shtml
map.xrezeku.cn/Blog/737559.shtml
map.xrezeku.cn/Blog/866068.shtml
map.xrezeku.cn/Blog/151175.shtml
map.xrezeku.cn/Blog/159579.shtml
map.xrezeku.cn/Blog/779379.shtml
map.xrezeku.cn/Blog/826022.shtml
map.xrezeku.cn/Blog/157171.shtml
map.xrezeku.cn/Blog/573157.shtml
map.xrezeku.cn/Blog/537713.shtml
map.xrezeku.cn/Blog/804462.shtml
map.xrezeku.cn/Blog/177737.shtml
map.xrezeku.cn/Blog/888668.shtml
map.xrezeku.cn/Blog/626824.shtml
map.xrezeku.cn/Blog/260442.shtml
map.xrezeku.cn/Blog/377375.shtml
map.xrezeku.cn/Blog/555953.shtml
map.xrezeku.cn/Blog/628460.shtml
map.xrezeku.cn/Blog/331913.shtml
map.xrezeku.cn/Blog/444802.shtml
map.xrezeku.cn/Blog/177571.shtml
map.xrezeku.cn/Blog/959159.shtml
map.xrezeku.cn/Blog/977599.shtml
map.xrezeku.cn/Blog/579559.shtml
map.xrezeku.cn/Blog/959173.shtml
map.xrezeku.cn/Blog/846022.shtml
map.xrezeku.cn/Blog/844000.shtml
map.xrezeku.cn/Blog/191173.shtml
map.xrezeku.cn/Blog/199771.shtml
map.xrezeku.cn/Blog/597177.shtml
map.xrezeku.cn/Blog/800668.shtml
map.xrezeku.cn/Blog/288286.shtml
map.xrezeku.cn/Blog/086082.shtml
map.xrezeku.cn/Blog/335575.shtml
map.xrezeku.cn/Blog/488880.shtml
map.xrezeku.cn/Blog/486222.shtml
map.xrezeku.cn/Blog/511373.shtml
map.xrezeku.cn/Blog/955177.shtml
map.xrezeku.cn/Blog/179559.shtml
map.xrezeku.cn/Blog/268024.shtml
map.xrezeku.cn/Blog/515551.shtml
map.xrezeku.cn/Blog/319195.shtml
map.xrezeku.cn/Blog/446402.shtml
map.xrezeku.cn/Blog/333739.shtml
map.xrezeku.cn/Blog/840884.shtml
map.xrezeku.cn/Blog/664284.shtml
map.xrezeku.cn/Blog/173915.shtml
map.xrezeku.cn/Blog/119715.shtml
map.xrezeku.cn/Blog/953959.shtml
map.xrezeku.cn/Blog/882686.shtml
map.xrezeku.cn/Blog/131133.shtml
map.xrezeku.cn/Blog/173755.shtml
map.xrezeku.cn/Blog/315793.shtml
map.xrezeku.cn/Blog/931739.shtml
map.xrezeku.cn/Blog/317179.shtml
map.xrezeku.cn/Blog/420082.shtml
map.xrezeku.cn/Blog/339193.shtml
map.xrezeku.cn/Blog/935333.shtml
map.xrezeku.cn/Blog/060066.shtml
map.xrezeku.cn/Blog/777999.shtml
map.xrezeku.cn/Blog/599759.shtml
map.xrezeku.cn/Blog/040262.shtml
map.xrezeku.cn/Blog/600484.shtml
map.xrezeku.cn/Blog/591311.shtml
map.xrezeku.cn/Blog/151955.shtml
map.xrezeku.cn/Blog/844444.shtml
map.xrezeku.cn/Blog/466262.shtml
map.xrezeku.cn/Blog/115753.shtml
map.xrezeku.cn/Blog/997931.shtml
map.xrezeku.cn/Blog/602602.shtml
map.xrezeku.cn/Blog/171933.shtml
map.xrezeku.cn/Blog/311537.shtml
map.xrezeku.cn/Blog/533331.shtml
map.xrezeku.cn/Blog/951999.shtml
map.xrezeku.cn/Blog/719353.shtml
map.xrezeku.cn/Blog/200600.shtml
map.xrezeku.cn/Blog/599753.shtml
map.xrezeku.cn/Blog/313599.shtml
map.xrezeku.cn/Blog/191535.shtml
map.xrezeku.cn/Blog/113159.shtml
map.xrezeku.cn/Blog/975993.shtml
map.xrezeku.cn/Blog/973377.shtml
map.xrezeku.cn/Blog/319311.shtml
map.xrezeku.cn/Blog/535111.shtml
map.xrezeku.cn/Blog/464444.shtml
map.xrezeku.cn/Blog/935739.shtml
map.xrezeku.cn/Blog/779359.shtml
map.xrezeku.cn/Blog/995133.shtml
map.xrezeku.cn/Blog/020020.shtml
map.xrezeku.cn/Blog/866840.shtml
map.xrezeku.cn/Blog/175357.shtml
map.xrezeku.cn/Blog/800204.shtml
map.xrezeku.cn/Blog/597339.shtml
map.xrezeku.cn/Blog/408480.shtml
map.xrezeku.cn/Blog/911757.shtml
map.xrezeku.cn/Blog/779733.shtml
map.xrezeku.cn/Blog/531755.shtml
map.xrezeku.cn/Blog/371177.shtml
map.xrezeku.cn/Blog/793937.shtml
map.xrezeku.cn/Blog/208206.shtml
map.xrezeku.cn/Blog/991735.shtml
map.xrezeku.cn/Blog/173559.shtml
map.xrezeku.cn/Blog/557355.shtml
map.xrezeku.cn/Blog/668224.shtml
map.xrezeku.cn/Blog/044240.shtml
map.xrezeku.cn/Blog/113173.shtml
map.xrezeku.cn/Blog/648664.shtml
map.xrezeku.cn/Blog/557713.shtml
map.xrezeku.cn/Blog/426204.shtml
map.xrezeku.cn/Blog/975331.shtml
map.xrezeku.cn/Blog/622246.shtml
map.xrezeku.cn/Blog/555757.shtml
map.xrezeku.cn/Blog/177159.shtml
map.xrezeku.cn/Blog/733375.shtml
map.xrezeku.cn/Blog/084660.shtml
map.xrezeku.cn/Blog/771515.shtml
map.xrezeku.cn/Blog/119759.shtml
map.xrezeku.cn/Blog/488422.shtml
map.xrezeku.cn/Blog/402440.shtml
map.xrezeku.cn/Blog/868868.shtml
map.xrezeku.cn/Blog/919771.shtml
map.xrezeku.cn/Blog/195797.shtml
map.xrezeku.cn/Blog/755991.shtml
map.xrezeku.cn/Blog/573793.shtml
map.xrezeku.cn/Blog/375797.shtml
map.xrezeku.cn/Blog/757355.shtml
map.xrezeku.cn/Blog/155171.shtml
map.xrezeku.cn/Blog/939131.shtml
map.xrezeku.cn/Blog/357179.shtml
map.xrezeku.cn/Blog/084846.shtml
map.xrezeku.cn/Blog/440820.shtml
map.xrezeku.cn/Blog/313331.shtml
map.xrezeku.cn/Blog/282444.shtml
map.xrezeku.cn/Blog/246060.shtml
map.xrezeku.cn/Blog/737539.shtml
map.xrezeku.cn/Blog/244088.shtml
map.xrezeku.cn/Blog/515313.shtml
map.xrezeku.cn/Blog/115377.shtml
map.xrezeku.cn/Blog/688262.shtml
map.xrezeku.cn/Blog/515999.shtml
map.xrezeku.cn/Blog/975771.shtml
map.xrezeku.cn/Blog/393595.shtml
map.xrezeku.cn/Blog/820448.shtml
map.xrezeku.cn/Blog/377557.shtml
map.xrezeku.cn/Blog/575197.shtml
map.xrezeku.cn/Blog/355559.shtml
map.xrezeku.cn/Blog/755593.shtml
map.xrezeku.cn/Blog/068806.shtml
map.xrezeku.cn/Blog/155733.shtml
map.xrezeku.cn/Blog/242804.shtml
map.xrezeku.cn/Blog/511573.shtml
map.xrezeku.cn/Blog/133739.shtml
map.xrezeku.cn/Blog/648088.shtml
map.xrezeku.cn/Blog/460608.shtml
map.xrezeku.cn/Blog/804266.shtml
map.xrezeku.cn/Blog/595959.shtml
map.xrezeku.cn/Blog/355139.shtml
map.xrezeku.cn/Blog/866248.shtml
map.xrezeku.cn/Blog/373935.shtml

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/2 22:07:07

HuggingFace Dataset加载VibeVoice训练数据样本

HuggingFace Dataset加载VibeVoice训练数据样本 在播客、有声书和虚拟角色交互日益普及的今天,用户对语音合成的要求早已超越“能读出来”的初级阶段。他们期待的是自然对话般的表达——有情绪起伏、有角色切换、有上下文理解,甚至能持续讲上几十分钟而不…

作者头像 李华
网站建设 2026/9/2 22:07:32

PHYFUSION对比传统CFD:效率提升的5个关键维度

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个PHYFUSION效率对比演示项目:1. 传统CFD方法(需手动划分网格)与PHYFUSION的AI自动建模对比;2. 相同翼型气动分析案例&#x…

作者头像 李华
网站建设 2026/9/2 22:05:57

10分钟原型:用快马平台验证RAM与ROM的不同行为

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 构建一个极简内存行为验证工具,功能:1.模拟RAM:浏览器刷新后数据丢失;2.模拟ROM:数据持久保存;3.并排对比界…

作者头像 李华
网站建设 2026/9/2 22:06:51

安装包依赖检查确保VibeVoice正常运行

安装包依赖检查确保VibeVoice正常运行 在播客、有声书和虚拟访谈等长时语音内容需求激增的今天,传统的文本转语音(TTS)系统正面临前所未有的挑战。用户不再满足于机械朗读式的单人旁白输出——他们需要的是自然对话感、角色一致性以及上下文连…

作者头像 李华
网站建设 2026/9/2 22:51:33

ComfyUI节点复制粘贴复用VibeVoice配置

ComfyUI节点复制粘贴复用VibeVoice配置 在播客制作、有声书生成和虚拟角色对话日益普及的今天,创作者面临一个共同难题:如何高效产出自然流畅、多角色参与且时长可观的语音内容?传统文本转语音(TTS)工具虽然能完成基本…

作者头像 李华
网站建设 2026/9/2 21:43:18

企业级应用:NEXT AI DRAWIO在项目管理中的实践

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个专为项目管理设计的NEXT AI DRAWIO扩展应用,包含项目管理常用图表模板库(如甘特图、泳道图)。要求支持从JIRA、Trello等工具导入数据自…

作者头像 李华