当前位置:网站首页>第二十四课 经典问题解析
第二十四课 经典问题解析
2022-04-23 11:55:00 【꧁༺夜༒雨༻꧂】
第二十四课 经典问题解析(构造析构顺序)
一、问题解析
-
问题:当程序中存在多个对象的时候,如何确定这些对象的析构顺序?
单个对象创建时构造函数的调用顺序
-
调用父类的构造过程
-
调用成员变量的构造函数(调用顺序与声明顺序相同)
一个类中包含其他类,调用顺序按照声明顺序来
-
调用类自身的构造函数
析构函数与对应的构造函数调用顺序相反
-
注意:以上三个并非独立的,而是按照这样的顺序调用的
对于栈对象和全局对象,类似于入栈与出栈的顺序,最后构造的对象最先析构
堆对象的析构发生在使用delete的时候,与delete的使用顺序相关
-
-
问题:const 关键字能否修饰类的对象?如果可以,有什么1特性?
const 关键字能够修饰对象
const 修饰的对象为只读对象
只读对象的成员变量不允许被改变
制度对象是编译阶段的概念,运行时无效
C++ 中的const成员函数
-
const对象只能调用 const 的成员函数
-
const 成员函数中只能调用 const 成员函数
-
const 成员函数中不能直接改写成员变量的值
-
const 成员函数定义:
type calssname::function(type p) const
注意:
const是在后面
类中的函数声明与史记函数定义中都必须带 const 关键字
-
-
问题:成员函数和成员变量都隶属于具体对象吗?
从面向对象角度
- 对象由属性(成员变量)和方法(成员函数)构成
从程序运行角度
- 对象由数据和函数构成,数据可以位于堆、栈、和全局数据区,函数只能位于代码段
所以是所有的对象共享一套成员函数,因为成员函数不能被删除
结论
- 每一个对象拥有自己的独立属性(成员变量)
- 所有的对象共享类的方法(成员函数)
- 方法能够直接访问对象的属性
- 方法中的隐藏参数 this 用于指代当前对象
成员函数只有一套,成员函数可以访问任何所属类里面的成员变量
二、小结
- 对象的析构顺序和构造顺序相反
- const 关键字能够修饰对象,得到只读对象
- 只读对象只能调用 const 成员函数
- 所有的对象共享类的成员函数
- 隐藏的 this 指针用于表示当前对象
版权声明
本文为[꧁༺夜༒雨༻꧂]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_34355238/article/details/124332232
边栏推荐
- Next.js 静态数据生成以及服务端渲染的方式
- ES6学习笔记二
- Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
- User interface and im expression (IM 5.6)
- Castle. Dynamic proxy implements transaction unit control
- 一文详解头部位姿估计【收藏好文】
- Interpretation of biological recognition in robot programming course
- Siri gave the most embarrassing social death moment of the year
- Yunna | fixed assets inventory supports multiple inventory methods (asset inventory)
- VMware virtual machines export hard disk vmdk files using esxi
猜你喜欢
rebbitMQ的简单搭建
Advanced file IO of system programming (13) -- IO multiplexing - Select
欣旺达宣布电池产品涨价 此前获“蔚小理”投资超10亿
Laravel增加自定义助手函数
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
The way to change children's programming structure
Relu function of activation function
软银愿景基金进军Web3安全行业 领投CertiK 6000万美元新一轮投资
PSCP 基本使用
Study notes of C [8] SQL [1]
随机推荐
获取钉钉考勤机打卡记录
Here comes the detailed picture and text installation tutorial of H5 game
The fourth chapter is the enable and disable columns of IM enabled fill objects (Part III of im-4.3)
Golang Pen & interview 01
IDEA 代码质量规范插件SonarLint
Analyze the rules for the use of robots with good performance
kettle复制记录到结果和从结果获取记录使用
Tan Xiang, CEO of Kechuang · Pera software: the essence of zero trust is digital security. To B should also deeply study the user's mind
Windows11 安装MySQL服务 提示:Install/Remove of the Service Denied
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
Nacos Foundation (9): Nacos configuration management from single architecture to microservices
How to count fixed assets and how to generate an asset count report with one click
Who said you should know PS? This open-source artifact can also be pulled in batch, and the effect is outstanding!
IDEA 代码格式化插件Save Actions
VMware虚拟机使用esxi 导出硬盘vmdk文件
远程访问家里的树莓派(上)
数据库如何填充IM表达式(IM 5.4)
运行报错:找不到或无法加载主类 com.xxx.Application
Advanced file IO of system programming (13) -- IO multiplexing - Select
IM表达式的目的(IM 5.2)