news 2026/4/18 8:37:12

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/4/18 8:32:36

VibeVoice集成大语言模型,实现上下文感知的语音合成

VibeVoice集成大语言模型,实现上下文感知的语音合成 在播客、有声书和虚拟访谈日益流行的内容生态中,一个长期困扰开发者的问题是:如何让AI生成的语音听起来不像“机器念稿”,而更像一场真实、自然的人类对话?传统的文…

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

AI一键搞定Jupyter Notebook安装,告别复杂配置

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 创建一个AI辅助的Jupyter Notebook安装工具。功能包括:1.自动检测用户操作系统类型和版本 2.根据系统环境生成定制化的安装脚本 3.自动安装Python和必要依赖项 4.配置J…

作者头像 李华
网站建设 2026/4/15 12:08:47

效率坊实战:用AI解决实际开发难题

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个实战应用,展示效率坊在数据处理中的能力。功能包括:从CSV或Excel导入数据,通过自然语言描述生成数据处理脚本(如过滤、排序…

作者头像 李华
网站建设 2026/4/18 5:33:52

电商系统MySQL实战:从安装到高可用集群搭建

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个电商系统MySQL部署模拟器,包含:1. 单节点安装演示 2. 主从复制配置向导 3. 读写分离中间件集成 4. 压力测试模块 5. 性能监控面板。要求使用Docker…

作者头像 李华
网站建设 2026/4/18 8:15:10

文本转图片工具如何提升内容创作效率300%

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个效率对比工具,功能包括:1. 传统设计流程时间记录器 2. AI文本转图片用时统计 3. 质量评估系统(分辨率、美观度等) 4. 批量处理性能测试 5. 生成详细…

作者头像 李华
网站建设 2026/4/18 5:42:12

League Akari:英雄联盟玩家的终极智能助手

League Akari:英雄联盟玩家的终极智能助手 【免费下载链接】LeagueAkari ✨兴趣使然的,功能全面的英雄联盟工具集。支持战绩查询、自动秒选等功能。基于 LCU API。 项目地址: https://gitcode.com/gh_mirrors/le/LeagueAkari 还在为繁琐的游戏操作…

作者头像 李华